[英]setInterval callback that calls another callback function is not working
Here is my code这是我的代码
function doIt(context, startTime) {
context.url((currentURL) => {
console.log("URL: " + currentURL.value)
})
console.log("running timer function ")
if (new Date() - startTime > 5000) { // timeout = 5 seconds
console.log("timed out")
clearInterval(timerId) // exit the loop
} else {
console.log("keep on truckin'")
}
}
let timerId = setInterval(doIt, 1000, this, Date.now())
Instead of looping five times (once per second for 5 seconds), it is only running once and the output comes out as...它不是循环五次(每秒一次,持续 5 秒),而是只运行一次,结果 output 显示为...
running timer function
keep on truckin'
URL: https://michigan.magellanrx.com/
If I comment out the first 3 lines of code (the context.url function) then the code runs as expected with the output...如果我注释掉前 3 行代码(context.url 函数),那么代码将按预期运行 output...
running timer function
keep on truckin'
running timer function
keep on truckin'
running timer function
keep on truckin'
running timer function
keep on truckin'
running timer function
timed out
Why isn't the code running through all five iterations when the context.url function isn't commented out?当 context.url function 没有被注释掉时,为什么代码没有运行所有五次迭代?
Clarification on this... it turns out it's not running just once.对此进行澄清......事实证明它不只运行一次。 Indeed, what is happening is that the setInterval function is changing from a "blocking" function to a "non-blocking" function when those first 3 lines of code are included.
实际上,当包含前 3 行代码时,setInterval function 正在从“阻塞”function 变为“非阻塞”function。 So, instead of running through all five iterations before moving on, it's only running the first iteration then moving on to subsequent commands while the setInterval continues to run in the background.
因此,它不会在继续之前运行所有五次迭代,而是仅运行第一次迭代然后继续执行后续命令,同时 setInterval 继续在后台运行。
From your description of the symptom it sounds like you may have an infinite loop in context.url()
.根据您对症状的描述,听起来您可能在
context.url()
中有一个无限循环。
Infinite loops blocks the main thread, thereby causing no events to be processed.无限循环阻塞主线程,从而导致没有事件被处理。 You need to find and remove that infinite loop.
您需要找到并删除那个无限循环。
The way the javascript interpreter is implemented is usually something like the following: javascript 解释器的实现方式通常如下所示:
// pseudocode:
do {
eventHandlers = executeJavascript();
// end of execution
events = waitForAllIO(); // this actually blocks but it is waiting
// for ALL I/O instead of just one
if (events.timeout) {
foreach (callback from eventHandlers) {
if (callback is TIMEOUT_HANDLER) {
callback(events.timeout);
}
}
}
else {
foreach (event from events) {
foreach (callback from eventHandlers) {
if (callback is for event) {
callback(event);
}
}
}
}
} while (eventHandlers.length > 0)
If your code is executing an infinite loop, that means the interpreter gets stuck at the executeJavascript()
part.如果您的代码正在执行无限循环,则意味着解释器会卡在
executeJavascript()
部分。 That means no timers or.network packets or any other events gets processed.这意味着没有定时器或网络数据包或任何其他事件得到处理。 Which explains why your
setInterval
only produces an output once.这解释了为什么您的
setInterval
只产生一次 output 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.