您可以使用setInterval的异步调用的功能呢? [英] Can you use setInterval to call functions asynchronously?

查看:606
本文介绍了您可以使用setInterval的异步调用的功能呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code是从项目丝绸(微软示例应用程序)取
下面的发布方法循环直通事件回调的数组并执行每一个。而不是使用一个for循环的setInterval的来代替。

文档说这使得每个用户回调至previous回调结束前被调用。它是否正确?我以为浏览器不会允许的时间间隔内的函数的执行运行,直到它的所有执行之前已经完成了。

这真的不是做一个循环有什么不同?

  that.publish =功能(eventName的数据)
{
    变种上下文,intervalId,IDX = 0;
    如果(队列[eventName的])
    {
        intervalId = setInterval的(功能()
        {
            如果(队列[eventName的] [IDX])
            {
                上下文=队列[eventName的] [IDX] .context ||这个;
                排队[eventName的] [idx的] .callback.call(上下文,数据);
                IDX + = 1;
            }
            其他{clearInterval(intervalId); }
        },0);
    }


解决方案

使用的setInterval这里确实让的异步的执行排序,因为它的时间表,下一次的主执行线程可用于回调的执行。

这意味着回调的执行不应该阻止浏览器,因为任何其他的同步处理将发生回调之前(因为回调排定运行,只有当主执行线程都有一个备用毫秒) - 这是什么使这个构建比普通的更好的循环 - 事实上,回调不会阻止浏览器,并导致可怕的这个页面有一个的时间太长脚本错误

该调度模式的副作用是,超时也只是建议 - 这就是为什么他们在这里使用0

请参阅: http://ejohn.org/blog/how-javascript-timers -work /

The following code is taken from Project Silk (a Microsoft sample application) The publish method below loops thru an an array of event callBacks and executes each one. Instead of using a for loop setInterval is used instead.

The documentation says this allows each subscriber callback to be invoked before the previous callback finishes. Is this correct? I thought the browser would not allow the execution of the function inside the interval to run until all prior executions of it had finished.

Is this really any different than doing a for loop?

that.publish = function (eventName, data) 
{
    var context, intervalId, idx = 0;
    if (queue[eventName]) 
    {
        intervalId = setInterval(function () 
        {
            if (queue[eventName][idx]) 
            {
                context = queue[eventName][idx].context || this;
                queue[eventName][idx].callback.call(context, data);
                idx += 1;
            } 
            else { clearInterval(intervalId); }
        }, 0);
    }

解决方案

Using setInterval here does make the execution sort of "asynchronous", because it schedules the execution of the callback for the next time the main execution thread is available.

This means that the callback executions should not block the browser because any other synchronous processing will take place before the callbacks (because the callbacks are scheduled to run only when the main execution thread has a spare millisecond) - and that's what makes this construct "better" than a regular for loop - the fact that the callbacks won't block the browser and cause the dreaded "This page has a script that's taking too long" error.

The side effect of this scheduling pattern is that the timeout is only a "suggestion" - which is why they use 0 here.

See: http://ejohn.org/blog/how-javascript-timers-work/

这篇关于您可以使用setInterval的异步调用的功能呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆