0

我正在处理的代码是,一旦我单击一个按钮,两个电机将连续旋转,直到我按下另一个按钮停止。我希望能够按住一个按钮来旋转电机,但是一旦放开那个按钮,电机就会停止。

我的 remoteControl.php 文件的一部分:

$action = $_GET['action'];
$pin = mysql_real_escape_string($_GET['pin']);
        if ($action == "forward"){
        $setting = "1";
        mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='17';");
        mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='22';");
        mysql_close();
        header('Location: remoteControl.php'); ..........
........
<form action="remoteControl.php" method="get">
<input id="forward" type="image" src="uparrow.jpg" IMG STYLE="position:absolute; TOP:150px; LEFT:170px; WIDTH:50px; HEIGHT:50px;">
<input type=hidden name="action" value="forward">
</form>

我正在尝试使用的 JavaScript:

<head>
    <script type="text/javascript">
        function OnButtonDown (button) {
            "can't figure out what to put";
        }
        function OnButtonUp (button) {
            "can't figure out what to put";
        }

        function Init () {
            var button = document.getElementById ("forward");
            if (button.addEventListener) {  // all browsers except IE before version 9
                button.addEventListener ("mousedown", function () {OnButtonDown (button)}, false);
                button.addEventListener ("mouseup", function () {OnButtonUp (button)}, false);
            }
            else {
                if (button.attachEvent) {   // IE before version 9
                    button.attachEvent ("onmousedown", function () {OnButtonDown (button)});
                    button.attachEvent ("onmouseup", function () {OnButtonUp (button)});
                }
            }
        }
    </script>
</head>
<body onload="Init ()">
4

1 回答 1

1

鼠标按下时调用 OnButtonDown,鼠标抬起时调用 OnButtonUp。如果这是有效的,唯一的问题是你不知道在该功能上做什么才能用状态更新你的数据库(我想有某种控制器可以检查数据库上的状态并更新 GPIO状态)。

您需要调用文件 remoteControl.php(它需要一个 GET 参数来更新数据库)。这可以使用 jquery 中的 ajax 函数来完成。

<head>
<!-- First we include jquery library. In this case from Google CDN -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<script type="text/javascript">
    function OnButtonDown (button) {
        //We pass the parameter forward to remoteControl.php
         $.ajax({
            data:  action=forward,
            url:   'remoteControl.php',
            type:  'get',
            success:  function (response) {

            }
         });
    }
    function OnButtonUp (button) {
        //Here the same as in OnButtonDown but passing another parameter
    }

    function Init () {
        var button = document.getElementById ("forward");
        if (button.addEventListener) {  // all browsers except IE before version 9
            button.addEventListener ("mousedown", function () {OnButtonDown (button)}, false);
            button.addEventListener ("mouseup", function () {OnButtonUp (button)}, false);
        }
        else {
            if (button.attachEvent) {   // IE before version 9
                button.attachEvent ("onmousedown", function () {OnButtonDown (button)});
                button.attachEvent ("onmouseup", function () {OnButtonUp (button)});
            }
        }
    }
</script>

于 2013-04-18T15:06:05.280 回答