1(function() { 2 window.throttle = function(func, wait) { 3 var args, result, thisArg, timeoutId, lastCalled = 0; 4 5 function trailingCall() { 6 lastCalled = new Date; 7 timeoutId = null; 8 result = func.apply(thisArg, args); 9 } 10 return function() { 11 var now = new Date, 12 remaining = wait - (now - lastCalled); 13 14 args = arguments; 15 thisArg = this; 16 17 if (remaining <= 0) { 18 clearTimeout(timeoutId); 19 timeoutId = null; 20 lastCalled = now; 21 result = func.apply(thisArg, args); 22 } else if (!timeoutId) { 23 timeoutId = setTimeout(trailingCall, remaining); 24 } 25 return result; 26 }; 27 }; 28})();