Delay Component

A component for triggering functions after a given amount of time.

This syncs with Crafty's internal clock, and so should generally be preferred to using methods such as setTimeout.

Methods

Back to top

.cancelDelay()

public this.cancelDelay(Function callback)
callback

Method reference passed to .delay

The cancelDelay method will cancel a delay set previously.

Example

var doSomething = function(){
  Crafty.log("doing something");
};

// execute doSomething each 100 miliseconds indefinetely
var ent = Crafty.e("Delay").delay(doSomething, 100, -1);

// and some time later, cancel further execution of doSomething
ent.cancelDelay(doSomething);
Back to top

.delay()

public this.delay(Function callback, Number delay[, Number repeat[, Function callbackOff]])
callback

Method to execute after given amount of milliseconds. If reference of a method is passed, there's possibility to cancel the delay.

delay

Amount of milliseconds to execute the method.

repeat

(optional) How often to repeat the delayed function. A value of 0 triggers the delayed function exactly once. A value n > 0 triggers the delayed function exactly n+1 times. A value of -1 triggers the delayed function indefinitely. Defaults to one execution.

callbackOff

(optional) Method to execute after delay ends(after all iterations are executed). If repeat value equals -1, callbackOff will never be triggered.

The delay method will execute a function after a given amount of time in milliseconds.

It is not a wrapper for setTimeout.

If Crafty is paused, the delay is interrupted with the pause and then resume when unpaused

If the entity is destroyed, the delay is also destroyed and will not have effect.

Example

The simplest delay

Crafty.log("start");
Crafty.e("Delay").delay(function() {
  Crafty.log("100ms later");
}, 100, 0);

Delay with callbackOff to be executed after all delay iterations

Crafty.log("start");
Crafty.e("Delay").delay(function() {
  Crafty.log("100ms later");
}, 100, 3, function() {
  Crafty.log("delay finished");
});
Back to top

.pauseDelays()

public this.pauseDelays()

The pauseDelays method will pause all delays of this entity until resumed.

Example

var doSomething = function(){
  Crafty.log("doing something");
};

// execute doSomething each 100 miliseconds indefinetely
var ent = Crafty.e("Delay").delay(doSomething, 100, -1);

// and some time later, the gameplay is paused
ent.pauseDelays();
Back to top

.resumeDelays()

public this.resumeDelays()

The resumeDelays method will resume earlier paused delays for this entity

Example

var doSomething = function(){
  Crafty.log("doing something");
};

// execute doSomething each 100 miliseconds indefinetely
var ent = Crafty.e("Delay").delay(doSomething, 100, -1);

// and some time later, the gameplay is paused (or only
// a part of it is frozen)
ent.pauseDelays();

// the player resumes gameplay
ent.resumeDelays();