danzen / zimjs

ZIM JavaScript Canvas Framework - Code Creativity! Interactive Media For All.
Other
507 stars 47 forks source link

zim.timeout error on short-timed events #12

Closed StevenWarren closed 7 years ago

StevenWarren commented 7 years ago

When using zim.timeout with 100 or less set for the delay I am getting the following error:

zim_5.3.0.js:5 Uncaught TypeError: n.clear is not a function at o (zim_5.3.0.js:5) at Object.e.timeout (zim_5.3.0.js:5) ....

It appears that this is happening due to next() being called on in zim.timeout() before obj,clear() and obj.pause() are set.

Test: zim.timeout(10, function(){zog("this is a test")});

Possible Fix: Move the next() call after obj.clear and obj.pause are set.

` zim.timeout = function(time, call) {

    z_d("9.7");
    if (zot(call)) return;
    if (typeof call != 'function') return;
    if (zot(time)) time = 1000;
    time = zik(time);
    var obj = {startTime:Date.now(), time:0, paused:false};
    var lastTime = obj.startTime;
    function next() {
        var now = Date.now()
        obj.time += now - lastTime;
        lastTime = now;
        if (obj.time >= time) {
            (call)(obj);
            obj.clear();
            return;
        }
        obj.rid = requestAnimationFrame(next);
    }       

    obj.pause = function(state) {
        if (zot(state)) state = true;
        if (state) { // pausing
            cancelAnimationFrame(obj.rid);
        } else { // unpausing
            lastTime = Date.now();
            next();
        }
        obj.paused = state;
    }

    obj.clear = function() {
        if (obj) cancelAnimationFrame(obj.rid);
        for (var i in obj) {
            delete obj[i];
        }
        obj.pause = function() {};
        obj.clear = function() {};
    }
        next();
    return obj;

} `

The fix seems harmless, but not sure if this may have implications elsewhere in the code.

danzen commented 7 years ago

Yes - that looks good. Funny, was not getting the error on my computer. ZIM 5.3.1 has been patched with the change. Please let me know if that fixed it for you - and thanks!!

StevenWarren commented 7 years ago

LOL, the old "it works on my computer". It appears to be working, thanks for the great framework.