简体   繁体   English

使用javascript的mousedown事件

[英]mousedown event using javascript

The code I have working on is that once I click on a button, two motors would spin continuously until I hit a different button to stop. 我正在处理的代码是,一旦我单击一个按钮,两个电动机就会连续旋转,直到我按下另一个按钮才能停止。 I want to be able to press and hold a button to spin the motors, but once let go of that button, the motor stops. 我希望能够按住一个按钮来旋转电动机,但是一旦松开该按钮,电动机就会停止运转。

Portion of my remoteControl.php file: 我的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 I'm trying to get working: 我正在尝试使用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 ()">

When the mouse is down you call OnButtonDown and when it is up you call OnButtonUp. 鼠标按下时,您调用OnButtonDown,鼠标按下时,您调用OnButtonUp。 If that is working, the only issue is that you don't know what to do on that functions in order to update your DB with the status (I suppose that there is some kind of controller that check the status on the DB and update GPIO state). 如果那是可行的,唯一的问题是您不知道要使用该功能做什么才能用状态更新数据库(我想有些控制器可以检查数据库的状态并更新GPIO州)。

You need to call the file remoteControl.php (that takes a GET parameter to update the DB). 您需要调用文件remoteControl.php(使用GET参数来更新数据库)。 This can be done using ajax function from jquery. 这可以使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM