定时器工厂

/**
 * 定时器工厂
 * @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();
});*/

 

posted on 2014-07-09 17:00  坚壳  阅读(197)  评论(0编辑  收藏  举报