/** * 函数节流 - 限制函数被频繁调用 * @param {Function} fn [需要执行的函数] * @param {[type]} interval [限制多长的时间再重复执行fn] */ var throttle = function(fn, interval) { var __self = fn, timer, firstTime = true; return function() { var args = arguments, __me = this; if (firstTime) { __self.apply(__me, args); return firstTime = false; }; if (timer) { return false; }; timer = setTimeout(function() { clearTimeout(timer); timer = null; __self.apply(__me, args); }, interval || 500); }; }; // test function A() { console.log(‘A‘); }; var A2 = throttle(A, 1000); setInterval(A2, 100);
时间: 2024-10-12 22:42:12