2013-04-14 71 views
0

我一直在努力的代碼是,一旦我點擊一個按鈕,兩個電機會連續旋轉,直到我點擊不同的按鈕停止。我希望能夠按住按鈕來旋轉電機,但一旦鬆開那個按鈕,電機就會停止。使用javascript的mousedown事件

部分我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()"> 
+0

什麼 「馬達」 你在說什麼? – Ian

+0

那麼,你在尋找能夠使圖像旋轉的代碼? – iConnor

+0

對不起,不清楚。我正在研究Raspberry Pi。我使用它作爲Web服務器,樹莓還具有GPIO(輸出引腳),我正在使用它來驅動或旋轉直流電機。一旦我點擊uparrow圖像,它會發送命令來旋轉直流電機。 – Bros

回答

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> 

+0

你是男人!!!!!!得到按鈕工作!!!!!我確實必須使用「」來代替數據,網址和類型。因爲我正在使用圖像文件,所以現在只需要通過從按鈕更改爲圖像來進行一點擦亮。 – Bros

+0

Scratch that ...無需從按鈕更改爲圖像。可以了,好了。非常感謝Anjz。你是一個拯救生命的人。如何解決這個問題? – Bros

+0

如果你點擊你接受答案作爲正確答案,答案左側有一個「檢查」按鈕。 – Anjz