daily pastebin goal
14%
SHARE
TWEET

Untitled

a guest Jul 19th, 2018 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * 批次讓元素間隔時間執行
  3.  * @param  {int}   delayTimes 延遲的時間
  4.  * @param  {callable} callback   對物件執行的動作
  5.  */
  6. $.fn.period = function (delayTimes, callback){
  7.    
  8.     var _total      = $(this).length,
  9.         _collection = $(this),
  10.         _delayTimes = delayTimes,
  11.         _callback   = callback;
  12.  
  13.     var loop = function (startIndex){
  14.  
  15.         // 取得元素
  16.         var target = _collection.get(startIndex);
  17.  
  18.         // 執行回呼,當等於 false 將會中斷前往下一個元素
  19.         var result = _callback.call(target, startIndex);
  20.  
  21.         if (result === false) return false;
  22.  
  23.         var nextIndex = startIndex + 1;
  24.  
  25.         // 下一個元素不存在則終止
  26.         if ($(_collection.get(nextIndex)).length == 0) return false;
  27.  
  28.         setTimeout(function (){
  29.            
  30.             loop(startIndex + 1)
  31.        
  32.         }, _delayTimes)
  33.     }
  34.  
  35.     loop(0);
  36. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top