Skip to content Skip to sidebar Skip to footer

How To Slow Down And Speed Up Time In JavaScript

I implemented an example of how to pause time in JavaScript. The example is here http://jsfiddle.net/suska/n4g5U/ // Update of Date class to modify getTime method. Date.prototype.o

Solution 1:

Wrote a variant on dooxe's answer to avoid the sudden jumps when changing between time warps.

var milli = Date.prototype.getTime;
var lastTime = (new Date).getTime();
var curTime = 0;
Date.prototype.getTime = function(){
    var actualTime = milli.call(this);
    curTime += (actualTime - lastTime) * Date._speed;
    lastTime = actualTime;
    return curTime;
};

Solution 2:

Here is a trick to 'override' Date.getTime function :

var milli = Date.prototype.getTime;
Date.prototype.getTime = function(){
    return milli.call(this) * 5;  
};

Use a factor (5 in my code) inferior to 1 to slow the time :

http://jsfiddle.net/uKk9R/


Post a Comment for "How To Slow Down And Speed Up Time In JavaScript"