Timer

Introduction

Execute callback when time-out, built-in object of phaser.

  • Author: Richard Davey

Usage

Start timer

  • Looped timer

    var timer = scene.time.addEvent({
        delay: 500,                // ms
        callback: callback,
        //args: [],
        callbackScope: thisArg,
        loop: true
    });
    
  • Repeat timer

    var timer = scene.time.addEvent({
        delay: 500,                // ms
        callback: callback,
        //args: [],
        callbackScope: thisArg,
        repeat: 4
    });
    
  • Oneshot timer

    var timer = scene.time.delayedCall(delay, callback, args, scope);  // delay in ms
    
  • All properties of timer

    var timer = scene.time.addEvent({
        delay: 500,                // ms
        callback: callback,
        args: [],
        callbackScope: thisArg,
        loop: false,
        repeat: 0,
        startAt: 0,
        timeScale: 1,
        paused: false
    });
    

Status of timer

  • Get elapsed time

    var elapsed = timer.getElapsed();   // ms
    var elapsed = timer.getElapsedSeconds(); // sec
    
  • Get repeat count

    var repeat = timer.getRepeatCount();
    
  • Gets the progress of the current iteration

    var progress = timer.getProgress();  // elapsed / delay
    
  • Gets the progress of the timer overall, factoring in repeats.

    var progress = timer.getOverallProgress();  // totalElapsed / totalDuration
    
  • Get delay time

    var delay = timer.delay;   // ms
    
  • Is paused

    var isPaused = timer.paused;
    
  • Get time-scale

    var timeScale = timer.timeScale;
    

Methods of timer

  • Pause timer
    timer.paused = true;
    
  • Resume timer
    timer.paused = false;
    
  • Remove timer from timeline
    timer.remove();
    
  • Set time-scale
    timer.timeScale = 2;