Shorthand for delayed execute (based on Tween.js)

I got tired of creating dummy tweens to be able to hook into the complete fucntion, so I made a little shortcut for it.
Put this anywhere (I put mine in EntityExtension.js).
It has the benefits of tweens, being cleaned up, depending on timescale, able to be killed.
You can add as many as you want to an entity.

pc.Entity.prototype.delayedExecute = function (duration, f, scope) {
    // find the first available tween slot
    // we do this to avoid accidentally overwriting tweens
    var n = 0;
    while(this["delayedExecuteTween" + n])
    // once we found an unused slot, we put a dummy tween in it
    // we're tweening dummy var m from 0 to 1, over 'duration' time.
    var id = "delayedExecuteTween" + n;
    var m;
    this[id] = this.tween(m)
        .to(1, duration, pc.Linear)
    // add the listener to the complete event
    // it removes itself from the slot when it's done, 
    // and executes the function with the right scope
    this[id].once("complete", function() {;
        this[id] = null;
    }, this);
    // return the tween, in case you want to use it for something
    return this[id];

and use it like this to execute myFunction 1 second from now.

this.entity.delayedExecute(1,this.myFunction, this);


Dude, that’s awesome. Thank you very much!