简体   繁体   English

调用另一个回调 function 的 setInterval 回调不起作用

[英]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.您需要找到并删除那个无限循环。


How javascript is implemented in C/C++ javascript在C/C++中是如何实现的

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.

相关问题 setInterval无法使用回调函数 - setInterval not working with callback function 如何等待 function 调用另一个回调 function - JavaScript - How to wait for a function that calls another callback function - JavaScript 将 JavaScript 回调传递给在另一个线程中调用它的 FFI 函数是否安全? - Is it safe to pass a JavaScript callback to an FFI function which calls it in another thread? setInterval 回调 function 不是动态的(Redux 道具) - setInterval callback function is not dynamic (Redux props) setInterval的回调函数,不带任何参数 - callback function with setInterval without any argument 回调 function 在 setInterval 的超时之前未完成 - Callback function not completed before setInterval's timeout setInterval,setTimeout和Node.js中的回调函数 - setInterval, setTimeout with callback function in Node.js 使用MochaJS在setInterval()中测试对外部函数的回调 - test a callback to an outside function with in setInterval() using MochaJS 从回调函数内部停止 setInterval - Stop setInterval from inside the callback function React-jest-enzyme:测试子组件的回调,该子组件在调用回调之前首先调用另一个函数 - React-jest-enzyme: testing callback of child component that calls another function first before calling the callback
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM