Last active
December 21, 2015 03:58
-
-
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?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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