英文:
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()
在页面渲染时调用。由于变量 myStartTime
和 myEndTime
可能会更改,因此在它们更改时需要再次调用该函数。但是这样会多次运行。
因此,我尝试用以下方式调用它:
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 myStartTims
and myEndTime
may 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
以下是已翻译的内容:
这里有一个 setRepeating
与 setInterval
有微妙差别的地方。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) => {
let timer;
const scheduleNext = () => {
timer = setTimeout(() => {
fn();
scheduleNext();
}, interval);
}
scheduleNext();
return () => {
clearTimeout(timer);
}
}
// And here's an example of how to use it:
// Log the date, then, one second later, do it again.
cancel = setRepeating(() => console.log(new Date()), 1000);
// as an example, cancel it after 5 seconds
setTimeout(() => 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('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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论