Skip to content

Instantly share code, notes, and snippets.

@notbend
Last active December 21, 2015 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notbend/6245627 to your computer and use it in GitHub Desktop.
Save notbend/6245627 to your computer and use it in GitHub Desktop.
could this be done without making a copy of the entire object in the update method?
var Stateful = function(states, init_state) {
if (typeof(states) === 'number') {
this.states = [];
for (var i = 0; i < states; i++) {
this.states[i] = "_" + i; //prefix with _ (_0,_1,_2...) so state labels are strings
}
} else {
this.states = states;
}
this.state = this.states[init_state];
this.last = this.state;
}
Stateful.prototype = {
get: function() {
return this.state;
},
update: function (new_state,delay) {
var self = this; //looking for a better way to do this
delay = (typeof(delay) === 'number') ? delay : 0;
if (delay === 0) {
this.last = this.state;
this.state = new_state;
} else {
window.setTimeout(function() {
self.update(new_state);
}, delay);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment