Skip to content

Instantly share code, notes, and snippets.

@ishiduca
Created March 23, 2012 07:37
Show Gist options
  • Save ishiduca/2168022 to your computer and use it in GitHub Desktop.
Save ishiduca/2168022 to your computer and use it in GitHub Desktop.
直列setTimer
// var t = new Timer;
// t.setTimeout(function () {
// console.log('start!');
// }, 250).setInterval(function () {
// var c = t.vars('count');
// if (c > 10) {
// console.log('finish!');
// t.stop();
// return;
// }
// console.log(c++);
// t.vars('count', c);
// }, 1000).start(function () {
// t.vars('count', 0);
// console.log('wait...');
// });
function Timer () {}
(function (tp) {
var _timeout = 'timeout';
tp.vars = function (p, v) {
if (! this.__vars__) this.__vars__ = {};
if (typeof v !== 'undefined') {
this.__vars__[p] = v;
return this;
}
return this.__vars__[p];
};
tp.setInterval = function (func, interval) {
return this.jobs(func, interval, 'interval');
};
tp.setTimeout = function (func, interval) {
var that = this;
return this.jobs(function () {
func.apply(that);
that.stop();
}, interval, _timeout);
};
tp.jobs = function (func, interval, typeOption) {
if (! this.__jobs__) this.__jobs__ = [];
if (typeof func === 'undefined') {
return this.__jobs__.shift();
}
if (typeof func === 'function') {
this.__jobs__.push({
func : func,
interval : (interval === interval - 0) ? interval : 1000,
type : (typeOption === _timeout) ? setTimeout : setInterval
});
}
return this;
};
tp.start = function (f) {
var job, _timer;
if (! this.timerID) {
if (typeof f === 'function') f();
if (! this.timerID) {
job = this.jobs();
if (job) {
_timer = job.type;
this.clearTimer = (job.type === _timeout) ? clearTimeout : clearInterval;
this.timerID = _timer(job.func, job.interval);
}
}
}
return this;
};
tp.stop = function () {
this.clearTimer(this.timerID);
delete this.timerID;
if (this.__jobs__ && this.__jobs__.length > 0) {
this.start();
}
return this;
};
})(Timer.prototype);
// var t, i = 0;
// t = simpletimer(function (clearTimer) {
// if (i > 10) {
// console.log('start wait');
// return clearTimer();
// }
// console.log(i);
// i++;
// }, 1000)(function (clearTimer) {
// console.log('stop wait');
// return clearTimer();
// }, 2000, 'timeout')(function (clearTimer) {
// if (i > 20) {
// console.log('FINISH');
// return clearTimer();
// }
// console.log(" * " + i);
// i++;
// }, 1000);
// t();
function simpletimer (f, interval, timerOption) {
var queue, _timerOption, that, helper;
queue = [];
_timerOption = 'timeout';
helper = function () {
var job, timerID, clearTimer;
if (queue.length > 0) {
job = queue.shift();
clearTimer = function () {
((job.setTimer === setTimeout)
? clearTimeout
: clearInterval)(timerID);
that();
};
timerID = job.setTimer(function () {
job.func(clearTimer);
}, job.interval);
}
};
that = function (f, interval, timerOption) {
var setTimer;
interval = (typeof interval === 'undefined') ? 1000 : interval;
setTimer = (timerOption === _timerOption) ? setTimeout : setInterval;
if (arguments.length === 0) {
helper();
} else if (typeof f === 'function' && interval === interval - 0) {
queue.push({
func : f,
interval : interval,
setTimer : setTimer
});
}
return that;
};
return that(f, interval, timerOption);
}
if (exports) {
exports.Timer = Timer;
exports.simpletimer = simpletimer;
}
@ishiduca
Copy link
Author

var timer = new Timer;

timer.setInterval(function () {
        var c = timer.vars('c');
        if (c > 10) {
            console.log('finish');
            timer.stop();
            return;
        }
        console.log(c);
        timer.vars('c', c + 1);
}, 1000).start(function () {
        timer.vars('c', 0);
        console.log('start');
});

@ishiduca
Copy link
Author

    var i, t;
    i = 0,
    t = simpletimer(function (clearTimer) {
            if (i > 10) {
                console.log('start wait');
                return clearTimer();
            }
            console.log(i);
            i++;
    }, 250)(function (clearTimer) {
            console.log('stop wait');
            return clearTimer();
    }, 2000, 'timeout')(function (clearTimer) {
            if (i > 20) {
                console.log('FINISH');
                return clearTimer();
            }
            console.log(" * " + i);
            i++;
    }, 250);

    t();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment