LoginSignup
5
5

More than 5 years have passed since last update.

開始と停止を制御出来るタイマー

Posted at

(・Θ・) oO( #bigCookieのclickを連打すれば…… )

コード

timer.js
function Timer(interval, fn) {
    this.interval = interval;
    this.fn = fn;
    this.tm = null;
    this.start = function() {
        if (!this.tm) {
            this.tm = setInterval(fn, this.interval);
        }
        return this;
    };
    this.stop = function() {
        clearInterval(this.tm);
        this.tm = null;
        return this;
    };
    return this;
}
timer.min.js
function Timer(a,b){this.interval=a;this.fn=b;this.tm=null;this.start=function(){if(!this.tm){this.tm=setInterval(b,this.interval)}return this};this.stop=function(){clearInterval(this.tm);this.tm=null;return this};return this};

使用例

どんどこカウント

var count = 0;
window.tm = new Timer(1, function() { console.log(count++); });
window.tm.start();

止める時はwindow.tm.stop()で。

何かのクッキーを連打

new Timer(100, document.querySelector('#bigCookie').onclick).start();

(^ω^) ?

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5