简体   繁体   English

Javascript-动态更改setTimeout / setInterval的间隔值

[英]Javascript - Dynamically changing interval value of setTimeout/setInterval

Can anyone please explain this code? 谁能解释这个代码?

var checkinterval = 1;
var myFunction = function(){
    clearInterval(Interval2);
    console.log(checkinterval++);
    setInterval(myFunction, 5000);  
}
var Interval2 = setInterval(myFunction, 1000);

This code is calling a function within its own definition. 该代码在其自己的定义内调用一个函数。 ie inside myFunction , there is setInterval(myFunction, 5000) which is calling the function inside its own definition. 即在myFunction内部,有setInterval(myFunction,5000)在其自己的定义内调用该函数。 That does not make sense. 那没有意义。

Similarly in clearInterval(Interval2) , the variable Interval2 is being called before its own definition. 类似地,在clearInterval(Interval2)中变量Interval2在其自己的定义之前被调用。

I am sorry but I am just a newbie with these timers. 抱歉,我只是这些计时器的新手。 Can someone please elaborate these bits of lines of code? 有人可以详细说明这些代码行吗?

From my understanding: 据我了解:

When you stored the function to the 'myFunction' variable, it is not executed immediately. 将函数存储到“ myFunction”变量时,不会立即执行。 They are simply stored for later use when you actually do invoke them. 它们只是存储起来供以后实际使用时使用。

This happens in the following line: 这发生在以下行中:

var Interval2 = setInterval(myFunction, 1000);

What this does is it invokes myFunction every 1 second. 这是每1秒钟调用一次myFunction。 In your case, however, it executes myFunction once only, because in myFunction the 但是,在您的情况下,它仅执行一次myFunction,因为在myFunction中,

clearInterval(Interval2)

line would have then cleared the timer assigned to the Interval2 variable. 行将清除分配给Interval2变量的计时器。

But the code does not just stop there. 但是代码并不止于此。 In myFunction, this line: 在myFunction中,此行:

setInterval(myFunction, 5000);

would call myFunction every 5 seconds, which increments your checkInterval by 1 and outputs it to the console. 会每5秒钟调用一次myFunction,这会使您的checkInterval增加1,并将其输出到控制台。 This is what causes your browser to crash, because if you think about it: 这是导致浏览器崩溃的原因,因为如果您考虑一下:

First call of myFunction: 1 instance of myFunction would be queued to run in the next 5s (lets call this A) 首次调用myFunction:1个myFunction实例将排队等待在接下来的5秒内运行(将其称为A)

5s later... 5秒后...

A is executed, which increments checkInterval by 1, and queues ANOTHER instance of myFunction to run in 5s (call this B) 执行A,将checkInterval递增1,并将myFunction的另一个实例排队等待5秒运行(称为B)

5s later... 5秒后...

A is executed, which increments checkInterval by 1, and queues ANOTHER instance of myFunction to run in 5s (call this C) 执行A,将checkInterval递增1,并将myFunction的另一个实例排队等待在5秒钟内运行(称为C)

A is executed, which increments checkInterval by 1, and queues ANOTHER instance of myFunction to run in 5s (call this D) 执行A,将checkInterval递增1,并将myFunction的另一个实例排队等待在5秒钟内运行(称为D)

5s later... 5秒后...

A is executed... B is executed... C is executed... D is executed... A被执行... B被执行... C被执行... D被执行...

You get the drift. 你得到的漂移。

Essentially what this code does is double the number of 'myFunction's to be executed every 5s. 本质上,此代码的作用是每5秒执行一次“ myFunction”的次数增加一倍。 Eventually there would be so many of them to run, your browser would naturally be unable to handle the load. 最终它们将运行很多,您的浏览器自然将无法处理负载。

Hope my explanation did not confuse you. 希望我的解释不会使您感到困惑。 I'll try to clarify any doubts you might have. 我会尽力澄清您可能有的任何疑问。

var checkinterval = 1; // global variable
var myFunction = function(){ // function in a variable
    clearInterval(Interval2); // stops Interval2 which loops every second
    console.log(checkinterval++); // gives the times how often it loops
    setInterval(myFunction, 5000); // myFuntion now loops every 5 seconds and stacks over and over couse the code will multiple
}
var Interval2 = setInterval(myFunction, 1000); // makes the snowball rolling after 1 sec.(calls the myFunction variable which has a function)

To make it run just go with IF(first loop?) 要使其运行,只需使用IF(first loop?)

var checkinterval = 1; // global variable
var Interval2;
var myFunction = function(){ // function in a a variable
    if(checkinterval == 1){
        setInterval(myFunction, 5000); // myFuntion now loops every 5 seconds
    }

    console.log(checkinterval++); // gives the times how often it loops
}
Interval2 = setTimeout(myFunction, 1000); // makes the snowball rolling after 1 sec.(calls the myFunction variable which has a function)

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

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