UNPKG

litejs

Version:

Single-page application framework

56 lines (48 loc) 1.08 kB
!function(F) { // Time to live - Run *onTimeout* if Function not called on time F.ttl = function(ms, onTimeout, scope) { var fn = this , tick = setTimeout(function() { ms = 0 if (onTimeout) onTimeout() }, ms) return function() { clearTimeout(tick) if (ms) fn.apply(scope, arguments) } } // Run Function one time after last call F.once = function(ms, scope) { var tick, args , fn = this return function() { clearTimeout(tick) args = arguments tick = setTimeout(function() { fn.apply(scope, args) }, ms) } } // Maximum call rate for Function // leading edge, trailing edge F.rate = function(ms, last_call, _scope) { var tick, args , fn = this , scope = _scope , next = 0 return function() { var now = Date.now() clearTimeout(tick) if (_scope === void 0) scope = this if (now >= next) { next = now + ms fn.apply(scope, arguments) } else if (last_call) { args = arguments tick = setTimeout(function() { fn.apply(scope, args) }, next - now) } } } }(Function.prototype)