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.origGetTime = Date.prototype.getTime;
Date._lastPausedAt;
Date._stopDuration = 0;
Date.prototype.getTime = function() {
if (Date._lastPausedAt) {
return Date._lastPausedAt.origGetTime() - Date._stopDuration;
}
return new Date().origGetTime() - Date._stopDuration;
};
Date.isPaused = function() {
return Date._lastPausedAt != null;
};
Date.pause = function() {
if (!Date._lastPausedAt) {
Date._lastPausedAt = new Date();
}
};
Date.unpause = function() {
if (Date._lastPausedAt) {
Date._stopDuration += new Date().origGetTime() - Date._lastPausedAt.origGetTime();
Date._lastPausedAt = null;
}
};
Any idea how can I modify the example to do a slow down and speed up functionality?
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;
};
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 :
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.