[英]How to execute code continuously in javascript?
I have a HTML-file and a PHP-file.我有一个 HTML 文件和一个 PHP 文件。 The index.php displays a value which gets transferred to the servo.php.
index.php 显示一个值,该值被传输到servo.php。 The servo.php writes the value in a file called /dev/servoblaster.
servo.php 将值写入名为 /dev/servoblaster 的文件中。
Currently the value only gets displayed if i click the button with my function.目前,只有当我单击带有我的功能的按钮时才会显示该值。 I would like that the value is displayed and sent continuously and not by the press of a button.
我希望该值能够连续显示和发送,而不是通过按下按钮。
The two files can you find here: servo.php index.html这两个文件可以在这里找到: servo.php index.html
The function: (You will find it also in the HTML-file)函数:(你也会在 HTML 文件中找到它)
function tilt() { var tilt = document.getElementById("tiltRange").value;
document.getElementById("tilt_Range").innerHTML = tilt;
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "servo.php?dir=P1-11=" + tilt, true);
xhttp.send();
}
The body tag of the HTML-file: HTML 文件的 body 标签:
<button onclick="tilt()">Tilt</button>
<input type="range" id="tiltRange" value="140" max="170" min="60">
<text id="tilt_Range"></text>
You could use setInterval
to call your script every n
seconds:您可以使用
setInterval
每n
秒调用一次脚本:
setInterval(tilt, 5000);
Will call tilt()
every 5 seconds...将每 5 秒调用一次
tilt()
...
EDIT:编辑:
Added clearInterval
example:添加了
clearInterval
示例:
var doIt = setInterval(tilt, 5000);
You can then clear / stop it like so:然后您可以像这样清除/停止它:
clearInterval(doIt)
So you should assign a click handler to your "stop" button, that calls the clearInterval.所以你应该为你的“停止”按钮分配一个点击处理程序,它调用 clearInterval。
You can use setInterval to execute any code at certain time interval.您可以使用 setInterval 以特定时间间隔执行任何代码。
setInterval(function () {
document.getElementById("tiltRange").value;
document.getElementById("tilt_Range").innerHTML = tilt;
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "servo.php?dir=P1-11=" + tilt, true);
xhttp.send();
},30000);
The answer by "Dhara Parmar" on this page worked for me.此页面上“Dhara Parmar”的回答对我有用。
I needed a scroll event trigger, such as when a user scroll downs to a web page of a certain position, an event would occur, such as a pop out or an alert message.我需要一个滚动事件触发器,例如当用户向下滚动到某个位置的网页时,会发生一个事件,例如弹出或警报消息。
Here is an example:下面是一个例子:
setInterval(function ()
{
if ($(window).scrollTop() > 1200) // at what scroll position
{
alert("hello");
}
}, 10); // time repetition interval in milliseconds
You could you use setInterval in this case, using the following syntax:在这种情况下,您可以使用 setInterval ,使用以下语法:
var interval = setInterval(function ()
{
var tilt = document.getElementById("tiltRange").value;
document.getElementById("tilt_Range").innerHTML = tilt;
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "servo.php?dir=P1-11=" + tilt, true);
xhttp.send();
}, 5000);
And when you want to stop it from doing that action, simply do:当您想阻止它执行该操作时,只需执行以下操作:
clearInterval(interval);
Hope that helped :) Let me know if that's not the case希望有所帮助 :) 如果情况并非如此,请告诉我
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.