shm*_*613 6 javascript jquery prototypejs
有没有jQuery相当于原型的延迟?
我正在寻找一些会延迟脚本执行的东西,直到页面中的所有脚本都完成执行.
谢谢!
第二部分:有没有办法看到队列中是否有其他的setTimeout并且延迟执行直到它们发生之后?我在评论中看到,有时候setTimeout为0或1并不重要,因为它是不可预测的,哪个会先触发.
再次感谢!
我在下面接受的答案中找到了我使用的代码中的错误.切片调用需要在0而不是1上工作,因为在原型核心代码中,它接受额外的参数等待(0.01).最后的方法然后变成:
Function.prototype.deferFunc = function() {
var __method = this, args = Array.prototype.slice.call(arguments, 0);
return window.setTimeout(function() {
return __method.apply(__method, args);
}, 0.01);
}
Run Code Online (Sandbox Code Playgroud)
您可以使用vanilla JavaScript中提供的基本版本用于大多数目的setTimeout():
setTimeout(function() {
//do something
}, 0);
Run Code Online (Sandbox Code Playgroud)
jQuery用于动画的类似排队机制是回调和.delay()函数(使用setTimeout()下面的).
所做的就是以 0 超时defer执行内部函数。window.setTimeout
我确信你可以像这样实现它:
Function.prototype.defer = function() {
var __method = this, args = Array.prototype.slice.call(arguments, 1);
return window.setTimeout(function() {
return __method.apply(__method, args);
}, 0);
}
Run Code Online (Sandbox Code Playgroud)