简体   繁体   English

JavaScript:setInterval和clearInterval,哪种方法正确?

[英]JavaScript: setInterval and clearInterval, which way is correct?

Which way is correct and more efficient in using setInterval() and clearInterval()? 使用setInterval()和clearInterval()哪种方法正确,更有效?

1. 1。

something = setInterval(function() {
    try {
        ...load something
        clearInterval(something);
    } catch (e) {
      // error
    }
}, 5000);

2. 2。

 something = setInterval(function() {
    try {
        ...load something            
    } catch (e) {
      // error
    }
}, 5000);

setTimeout(something, 7000);

EDIT: 编辑:

For #2, I meant setTimeout() instead of clearInterval().Has been changed. 对于#2,我的意思是setTimeout()而不是clearInterval()。已更改。

I assume the interval you're passing into clearInterval is meant to be something . 我认为您要传递给clearIntervalinterval应该是something

Your second example will never fire your timer, because you clear the timer immediately after setting it. 您的第二个示例永远不会触发您的计时器,因为您在设置计时器后立即将其清除。 You're also passing an argument ( 7000 ) into clearInterval that won't get used ( clearInterval only accepts one argument). 您还将一个参数( 7000 )传递到clearInterval ,该参数将不被使用( clearInterval仅接受一个参数)。

Your first example is right provided that you want to clear the repeated timer at the point where you're calling clearInterval from within the handler. 第一个示例是正确的, 前提是您要在从处理程序内部调用clearInterval的点清除重复的计时器。 Presumably that's in an if or similar, because if you want a one-off timed callback you'd use setTimeout , not setInterval . 大概在if或类似的情况下,因为如果您想要一次性定时回调,则可以使用setTimeout ,而不要使用setInterval


EDIT: 编辑:

For #2, I meant setTimeout() instead of clearInterval().Has been changed. 对于#2,我的意思是setTimeout()而不是clearInterval()。已更改。

That completely changes the question. 这完全改变了问题。 No, that's not correct. 不,那是不正确的。 setInterval schedules the function to be called repeatedly on the interval you give it, you don't pass its return value into setTimeout . setInterval安排在给定的时间间隔内重复调用该函数,而不将其返回值传递给setTimeout

If you need something to happen over and over again you use setInterval if you only need it to happen once use setTimeout (you can simulate setInterval by chaining multiple timeouts one after the other). 如果您需要一遍又一遍地做某事,请使用setInterval如果只需要一次使用setTimeout进行一次,则可以使用setInterval (可以通过依次链接多个超时来模拟setInterval ))。 Timeouts only happen once therefore you do no need to clear them. 超时仅发生一次,因此您无需清除它们。 Also clearInterval does not take a time argument so the interval you set will be cleared before it ever executes since classic javascript is synchronous. 另外clearInterval不需要时间参数,因此您设置的时间间隔将在执行之前被清除,因为经典javascript是同步的。

just to complete the answer, take many care with setInterval(). 只是为了完成答案,请多加注意setInterval()。 if your "...load something" take sometime more time to load than the time according (for a reason or another). 如果您的“ ...加载某物”花费的时间比相应时间(出于某种原因)花费的时间更多。 it will just don't do it for this time and will wait the next call of setinterval. 它将暂时不执行此操作,并等待setinterval的下一次调用。 I encourage to use setTimeout() as much as possible instead. 我鼓励尽可能多地使用setTimeout()。

You can find find below the use cases that are, according to me, aswering to your questions: 根据我的看法,您可以在下面找到用例,它们正在询问您的问题:

For your case 1: 对于您的情况1:

var something = setInterval(function() {
    // Do stuff, and determine whether to stop or not

    if (stopCondition) {
        clearInterval(something);
    }
}, 5000);

For your case 2: 对于您的情况2:

var something = setInterval(function() {
    // Do stuff
}, 5000);

// Pass a function to setTimeout
setTimeout(function() {
    clearInterval(something);
}, 17000);

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

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