为什么一些JavaScript开发人员使用setTimeout一毫秒? [英] Why are some JavaScript developers using setTimeout for one millisecond?

查看:126
本文介绍了为什么一些JavaScript开发人员使用setTimeout一毫秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jQuery插件 tablesorter 时遇到问题,我无法再拨打两次触发器。

I have problem when using jQuery plugin tablesorter and I can't call trigger twice.

例如,这不起作用:

this._$table.trigger('update');
this._$table.trigger('sorton', [[[1,1]]]);

但这样可行:

this._$table.trigger('update');
setTimeout($.proxy(function() {
    this._$table.trigger('sorton', [[[1,1]]]);
}, this), 1);

然后我看到问题出现在触发器'update'中,它调用body方法:

And then I see that problem was in trigger 'update', it call method with body:

function () {
    var me = this;
    setTimeout(function () {
        // rebuild parsers.
        me.config.parsers = buildParserCache(
        me, $headers);
        // rebuild the cache map
        cache = buildCache(me);
    }, 1);
}

为什么tablesorter开发人员使用 setTimeout 一毫秒?

Why did the tablesorter developer use setTimeout with one millisecond?

推荐答案

这是一个老黑客。如果需要在另一个事件之后触发某个事件,您可以使用 setTimeout 并使用1ms来确保在另一个事件之后触发该事件。

It's an old hack. If an event needs to be triggered after another event you can use setTimeout with 1ms to make sure the event is triggered after the other event.

这篇关于为什么一些JavaScript开发人员使用setTimeout一毫秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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