/** * 定时器工厂 * @param {Number} interval [定时器间隔] * @param {Function} callback [定时执行的方法] * @param {Object} context [定时方法的作用域] * @param {Array} args [定时器方法的参数] * @return {Object} [定时器对象] */ function timeCreater(interval, callback, context, args){ var timer; function open() { timer = setInerval(function() { callback.apply(context || window, args); }, interval || 500); } function close() { if (timer) { clearInterval(timer); } } function set(inter) { interval = inter; close(); open(); } function get() { return interval; } return { get:get, set: set, open: open, close: close, } } /*demo: var time1 = timeCreater(100, function() { consolel.log(2); }); time1.open(); $(‘el‘).on(‘mouseout‘, function() { time1.open(); }).on(‘mouseover‘, function() { time1.close(); });*/
原文地址:http://www.cnblogs.com/feng524822/p/3833964.html