Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.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 setInterval()函数是如何工作的?_Javascript_Setinterval - Fatal编程技术网

Javascript setInterval()函数是如何工作的?

Javascript setInterval()函数是如何工作的?,javascript,setinterval,Javascript,Setinterval,在我们将这个函数放入变量中以便能够使用clearInterval()之后,为什么它不调用这个函数就可以工作呢 let time = 0; var timer = setInterval(()=>{ window.console.log(`Time passed: ${time}`); time = time+1; if(time>5){ clearInterval(timer); } }, 1000); 通常,当我们把一个函

在我们将这个函数放入变量中以便能够使用
clearInterval()
之后,为什么它不调用这个函数就可以工作呢

let time = 0;

var timer = setInterval(()=>{


    window.console.log(`Time passed: ${time}`);
    time = time+1;

    if(time>5){
        clearInterval(timer);
    }

}, 1000);
通常,当我们把一个函数放入变量中时,为了激发它,我们应该调用它。在这个例子中,我认为它应该是这样工作的:

timer();
但它不需要打电话就能工作


谢谢。

如果您真的在
timer
变量中放入一个函数,它会像您预期的那样工作,但是您没有。您正在调用
setInterval
函数并将其返回值存储到
timer
变量中

为了让它像您期望的那样工作,您需要将它放入另一个函数中,如下所示:

let time = 0;

var timer = function() {
    return setInterval(()=>{            
        window.console.log(`Time passed: ${time}`);
        time = time+1;

        if(time>5){
            clearInterval(timer);
        }
    }, 1000);
}

现在,调用
timer()
将按预期调用计时器

如果您实际在
timer
变量中放入函数,它会像您预期的那样工作,但是您没有。您正在调用
setInterval
函数并将其返回值存储到
timer
变量中

The clearInterval() function in javascript clears the interval which has been set by setInterval() function before that.

 - setInterval() function takes two arguments first one was the function that to be executed and the second argument was time (in ms).
 - setInterval() executes the passed function for the given time interval. The number id value returned by setInterval() function is stored in a variable and it’s passed into the clearInterval() function to clear the interval.

   let time = 0;

let timer = function() {
    return setInterval(()=>{            
        window.console.log(`Time passed: ${time}`);
        time = time+1;

        if(time>5){
            clearInterval(timer);
        }
    }, 1000);
}
为了让它像您期望的那样工作,您需要将它放入另一个函数中,如下所示:

let time = 0;

var timer = function() {
    return setInterval(()=>{            
        window.console.log(`Time passed: ${time}`);
        time = time+1;

        if(time>5){
            clearInterval(timer);
        }
    }, 1000);
}

现在,调用
timer()
将按预期调用计时器

您需要将setInterval包装为一个函数

The clearInterval() function in javascript clears the interval which has been set by setInterval() function before that.

 - setInterval() function takes two arguments first one was the function that to be executed and the second argument was time (in ms).
 - setInterval() executes the passed function for the given time interval. The number id value returned by setInterval() function is stored in a variable and it’s passed into the clearInterval() function to clear the interval.

   let time = 0;

let timer = function() {
    return setInterval(()=>{            
        window.console.log(`Time passed: ${time}`);
        time = time+1;

        if(time>5){
            clearInterval(timer);
        }
    }, 1000);
}
let time=0;
变量计时器=()=>setInterval(()=>{
log(`Time passed:${Time}`);
时间=时间+1;
如果(时间>5){
清除间隔(计时器);
}

}, 1000);您需要将setInterval包装为一个函数

let time=0;
变量计时器=()=>setInterval(()=>{
log(`Time passed:${Time}`);
时间=时间+1;
如果(时间>5){
清除间隔(计时器);
}

}, 1000);
您正在调用
setInterval
并将
setInterval
返回的引用存储到
timer
变量。您正在调用
setInterval
并将
setInterval
返回的引用存储到
timer
变量。
clearInterval(timer)
不会清除间隔,因为
计时器
是一个函数,而不是
设置间隔
的返回值。好的,是的。
timer()
调用的返回值需要存储在单独的变量中,该变量稍后可用于清除间隔。
clearInterval(timer)
不会清除间隔,因为
计时器
是一个函数,而不是
设置间隔
的返回值。好的,是的。
timer()
调用的返回值需要存储在单独的变量中,该变量稍后可用于清除间隔。