以下に、カウントを開始してから毎秒更新する単純なクラスがあります。特定の値をリッスンしてからコールバックを起動する機能を追加するにはどうすればよいですか?
function Counter() {
this.currentCount = 0;
}
Counter.prototype.start = function() {
setInterval(this.update, 1000);
};
Counter.prototype.when = function(value, callback) {
callback(value);
};
Counter.prototype.update = function() {
this.currentCount++;
};
私の考えでは、それはこのようなものになるでしょう。
var counter = new Counter();
counter.when(50, function(value) {
console.log('We arrived at ' + value + ', the requested value.');
});
counter.start();