函数节流,通过setTimeout来设置最小间隔时间,避免函数重复执行

   function Test()
   {
    document.getElementById("txtInput").value = arguments[0];
   }

   var Processor = (function(){
    function Start(fn, context, timeout)
    {
     var timeoutId = fn._timeoutId ? fn._timeoutId : null;     
     clearTimeout(timeoutId); //清除上一个Timeouts

     var args = Array.prototype.slice.call(arguments, 3);
     fn._timeoutId = setTimeout(function()
     {
      fn.apply(context, args);
     }, timeout);

    }
    return {Start : Start};
   })(this);
   for (var i = 0; i < 15; i++)
    Processor.Start(Test, null, 100, i);

posted on 2011-05-26 11:29  炼炁修士  阅读(854)  评论(0)    收藏  举报

导航