js 延期执行_js–延时执行

  • Post author:
  • Post category:其他


在js里面怎么延时执行一个函数?一般利用setInterval,setTimeout进行延迟异步执行,或者利用while循环,sleep()进行同步阻塞。

考虑到如果应用多次,可以针对Function全局对象进行扩展,为函数增加一个延时方法如delay,这样会让代码更简洁有效。

Function.prototype.delay = function(this1,timeout){

this1 = this1 || null;

timeout = timeout || 0;

var _this = this; // refer to function hi

var args = [];

console.log(‘delay is called’);

switch(arguments.length){

case 1:

timeout = parseInt(arguments[0]);

timeout = isNaN(timeout)? 0 :timeout;

timeout = timeout<0 ? 0 :timeout;

break;

default:

for(var i=0;i

console.log(‘args[‘+i+’]=’+arguments[i]);

if(i>1) args.push(arguments[i]);

};

break;

};

var proxy = function(){

_this.apply(



版权声明:本文为weixin_31688465原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。