2013-09-25 52 views
-1

我使用POSTs將命令發送給arduino。我希望在按住按鈕的同時重複向上命令。這是我正在嘗試...如何在按住按鈕的同時獲取查詢按鈕以重複發帖?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
<title></title> 

     <link rel="stylesheet" href="../templates/mainkibblynibblytemplate/css/kibbly-jquery.css" /> 
     <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
     <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> 
    <script type="text/javascript"> 
     var timeout, clicker = $('#upbutton'); 

     clicker.mousedown(function() { 
          timeout = setInterval(function() { 
               $.post('http://192.168.1.77:8888/', { 
                 text: 'up' 
                 }); 
               }, 500); 

          return false; 
          }); 

     $(document).mouseup(function() { 
          clearInterval(timeout); 
          return false; 
          }); 
     </script> 

</head> 
<body> 
<button id="upbutton">Test</button> 
</body> 
</html> 
+1

你有問題還是什麼? – asawyer

+0

看起來它應該可以正常工作(儘管每半秒ajax請求可能會很多)。你看到了什麼? –

+0

我已經編輯過以上所有的html。當按下按鈕時它不會發布。 – nonlinearmind

回答

0

您可以通過在JavaScript中使用onkeydown事件來實現此目的。在onkeydown事件中,你可以循環你想要做的結果。

0

您必須將事件mouseUp處理到一個按鈕,刪除「返回false」

這樣的:

clicker.mousedown(function() { 
    timeout = setInterval(function() { 
       $.post('http://192.168.1.77:8888/', { 
         text: 'up' 
       }); 
       }, 500); 
}); 

clicker.mouseup(function() { 
    clearInterval(timeout); 
}); 
+0

它仍然沒有發佈。 – nonlinearmind

+0

如果帖子出現在控制檯選項卡中,則必須使用螢火蟲查看。 if dont,the problem is the post code:$ .post('http://192.168.1.77:8888/',{ text:'up' }); –