停止一个无限循环并重新启动。

huangapple go评论73阅读模式
英文:

stop an infinite loop and restart it

问题

我创建了一个无限循环来检查是否达到特定时间并相应更改我的界面。

function infiniteloop(){
    let now = moment();
    start = moment(myStartTime);
    end = moment(myEndTime);
    if (now.isBetween(start, end)) {
        console.log('现在正在发生这个')
    } else  {
        console.log('现在没有发生这个')
    }
    setTimeout(infiniteloop, 5000);
}

infiniteloop() 在页面渲染时调用。由于变量 myStartTimemyEndTime 可能会更改,因此在它们更改时需要再次调用该函数。但是这样会多次运行。

因此,我尝试用以下方式调用它:

clearTimeout(infiniteloop);
infiniteloop();

但这并没有奏效。如何确保循环停止并重新调用?

英文:

I have created an infinite loop to check if a certain time is met and change my interface accordingly.

function infiniteloop(){
    let now = moment();
    start = moment(myStartTime);
    end = moment(myEndTime);
    if (now.isBetween(start, end)) {
        console.log('This is happening now')
    } else  {
        console.log('This is not happening now')
    }
    setTimeout(infiniteloop, 5000);
}

infiniteloop() is called when my page renders. As the variables myStartTimsand myEndTimemay change, it is necessary to call that function again when they change. But then it runs multiple times.

So I tried to call it with

clearTimeout(infiniteloop);
infiniteloop();

But that doesn't do the Trick. How can I ensure the loop is stopped and called again?

答案1

得分: 2

以下是已翻译的内容:

这里有一个 setRepeatingsetInterval 有微妙差别的地方。setInterval 调用会在固定时间间隔内执行某项操作,而不管函数本身需要多长时间。相比之下,setRepeating 调用将运行该函数,然后 在函数完成之后,它将等待完整的时间间隔再次运行它。

这就像烤蛋糕,然后等待一分钟,再烤另一个蛋糕,与试图每隔一分钟烤一个蛋糕之间的区别。

这也在功能上不同于 setInterval,因为该函数返回一个取消函数,而不是一个 ID,这在我看来更清晰。因为你所要做的唯一一件事就是取消计时器。

要 "重新启动" 它,只需再次调用 setRepeating 并获得一个新的取消函数。

英文:

Here's a setRepeating that is subtly different from setInterval. A setInterval call will do something at regular intervals regardless of how long the function itself takes. Whereas this setRepeating call will run the function, then after the function is done, it will wait for the full interval period before running it again.

It's the difference between baking a cake, then waiting one minute, then baking another cake, vs. trying to bake one cake every minute.

It's also functionally different from setInterval in that this function returns a cancellation function instead of an id, which is a little cleaner, IMO. Because the only thing you ever do with a timer id is cancel the timer.

To "restart" this, just call setRepeating again and get a new cancel function.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const setRepeating = (fn, interval) =&gt; {
    let timer;
    const scheduleNext = () =&gt; {
        timer = setTimeout(() =&gt; {
            fn();
            scheduleNext();
        }, interval);
    }
    scheduleNext();
    return () =&gt; {
        clearTimeout(timer);
    }
}

// And here&#39;s an example of how to use it:
// Log the date, then, one second later, do it again.
cancel = setRepeating(() =&gt; console.log(new Date()), 1000);

// as an example, cancel it after 5 seconds
setTimeout(() =&gt; cancel(), 5000);

<!-- end snippet -->

答案2

得分: 1

Sure, here's the translated code without the comments:

let timeoutId;

function infiniteloop(){
   let now = moment();
   start = moment(myStartTime);
   end = moment(myEndTime);
   if (now.isBetween(start, end)) {
       console.log('This is happening now');
   } else  {
       console.log('This is not happening now');
   }
   timeoutId = setTimeout(infiniteloop, 5000);
}

infiniteloop();
.....
myStartTime = anytime1;
myEndTime = anytime2;
// when change the myStartTime or myEndTime do:
.....
clearTimeout(timeoutId);
infiniteloop();
英文:
let timeoutId

function infiniteloop(){
   let now = moment();
   start = moment(myStartTime);
   end = moment(myEndTime);
   if (now.isBetween(start, end)) {
       console.log(&#39;This is happening now&#39;)
   } else  {
       console.log(&#39;This is not happening now&#39;)
   }
   timeoutId = setTimeout(infiniteloop, 5000);
}

infiniteloop()
.....
myStartTime = anytime1
myEndTime = anytime2
// when change the myStartTime or myEndTime do:
.....

clearTimeout(timeoutId)
infiniteloop()

huangapple
  • 本文由 发表于 2023年3月20日 22:53:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75791834.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定