Skip to content

Instantly share code, notes, and snippets.

@AGoblinKing
Last active August 29, 2015 13:59
Show Gist options
  • Save AGoblinKing/10935890 to your computer and use it in GitHub Desktop.
Save AGoblinKing/10935890 to your computer and use it in GitHub Desktop.
var TimeTraveler = (function() {
var time,
def = function(value) {
var values = {},
timeTraveler = {
get value() {
var lastTime = Object.keys(values)
.filter(function(pastTime) {
return pastTime < def.time;
})
.sort(function(a, b) { return a < b ? - 1 : a === b ? 0 : 1;})
.pop();
return values[lastTime];
},
set value(newValue) {
values[def.time] = newValue;
}
};
timeTraveler.value = value;
return timeTraveler;
};
Object.defineProperty(def, "time", {
get : function() {
return time === undefined ? (new Date()).getTime() : time;
},
set : function(pTime) {
time = pTime;
}
});
return def;
} ());
var foo = TimeTraveler(200);
setTimeout(function() {
foo.value = 500;
TimeTraveler.time = (new Date()).getTime() - 200;
console.log(foo.value);
}, 500);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment