Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 油脂猴子循环的java脚本_Javascript_Loops_Greasemonkey - Fatal编程技术网

Javascript 油脂猴子循环的java脚本

Javascript 油脂猴子循环的java脚本,javascript,loops,greasemonkey,Javascript,Loops,Greasemonkey,我需要的是一个for或while循环,它将每秒重新运行代码 我试过睡眠,但我认为它不起作用,或者我做对了 // where yourfunction is a method that contains your loop logic setTimeout(yourfunction, 1000); 这将每1000毫秒调用一次函数,而不必将其嵌入while或for循环 把它放在你的身体负荷或类似的事件中试试看 // where yourfunction is a method that conta

我需要的是一个for或while循环,它将每秒重新运行代码 我试过睡眠,但我认为它不起作用,或者我做对了

// where yourfunction is a method that contains your loop logic
setTimeout(yourfunction, 1000);
这将每1000毫秒调用一次函数,而不必将其嵌入while或for循环

把它放在你的身体负荷或类似的事件中

试试看

// where yourfunction is a method that contains your loop logic
setTimeout(yourfunction, 1000);
这将每1000毫秒调用一次函数,而不必将其嵌入while或for循环


将其放入身体onload或类似事件中

不要尝试使用
进行
循环进行此类定时操作。您将很难获得可靠或准确的计时,通常会导致CPU失控,导致计算机运行缓慢

JavaScript为这类任务提供了
setInterval()
函数。还要注意的是

因此,您需要的代码如下所示:

var timerVar    = setInterval (function() {DoMeEverySecond (); }, 1000);

function DoMeEverySecond ()
{
    //--- Your code here.
}

//--- When ready to stop the timer, run this code:
clearInterval (timerVar);
timerVar        = "";

不要尝试对
使用
,或在
循环时对此类定时操作使用
。您将很难获得可靠或准确的计时,通常会导致CPU失控,导致计算机运行缓慢

JavaScript为这类任务提供了
setInterval()
函数。还要注意的是

因此,您需要的代码如下所示:

var timerVar    = setInterval (function() {DoMeEverySecond (); }, 1000);

function DoMeEverySecond ()
{
    //--- Your code here.
}

//--- When ready to stop the timer, run this code:
clearInterval (timerVar);
timerVar        = "";

这应该是
setInterval
,而不是
setTimeout
。此外,如果函数需要参数,最好显示匿名函数语法。这应该是
setInterval
,而不是
setTimeout
。此外,最好显示匿名函数语法,以防函数需要参数。+1 brock,很好的一点是我的setTimeout。我当然是指设置间隔(同样,非常好的点参数!!)。手指比大脑快!!节日问候…:-)+布罗克,你说得对,我的设定超时了。我当然是指设置间隔(同样,非常好的点参数!!)。手指比大脑快!!节日问候…:-)