Fork me on GitHub

timestore is a lightweight library which replaces native setTimeout/setInterval methods. It allows you to create a store, set timers which belong to that store, and then run, clear, pause and resume them either one by one, or altogether. That makes it very useful to integrate timestore with game states by creating a store for each state and switching between them without losing timers from other stores.

Docs and code are here!


Yellow rocket will move 2 seconds after you set a Timeout. You can clear timeout and also pause and resume it with the "Toggle" button.

The number shows the time until launch.


var TS = new timestore.Timestore(),
    timeout;

$('#set_timeout_0').on('click', function () {
    timeout = TS.setTimeout('0th', function () {
        yellowRocket.accelerate();
    }, 2000);
});

$('#clear_timeout_0').on('click', function () {
    timeout && timeout.clear();
});

$('#toggle_timeout_0').on('click', function () {
    timeout && timeout.toggle();
});

Pink rocket will move constantly after you set Interval. When you clear it, the rocket will stop.


// <-- The same TS as on the left!

var interval;

$('#set_interval_1').on('click', function () {
    interval = TS.setInterval('1st', function () {
        pinkRocket.move();
    }, 50);
});

$('#clear_interval_1').on('click', function () {
    interval && interval.clear();
});

Those rockets will move constantly after you set Intervals. They will stop when you clear Intervals.

Also, you can toggle each rockets on and off. In this example, toggle works only when Intervals are set and not cleared.


// The method .clearAll() affects all timers.
// So we need another "timerspace".
var newTS = new timestore.Timestore();

// Intervals with custom IDs will be overwritten.
$('#set_intervals_2').on('click', function () {

    newTS.setInterval('blue', function () {
        blueRocket.move(1);
    }, 50);

    newTS.setInterval('green', function () {
        greenRocket.move(2);
    }, 50);

    newTS.setInterval('red', function () {
        redRocket.move(2);
    }, 25);

});

$('#clear_intervals_2').on('click', function () {
    newTS.clearAll();
});

$('#toggle_blue_2').on('click', function () {
    newTS.toggleInterval('blue');
});

$('#toggle_green_2').on('click', function () {
    newTS.toggleInterval('green');
});

$('#toggle_red_2').on('click', function () {
    newTS.toggleInterval('red');
});