[英]Executing some function when the state of a variable changes?
我正在嘗試在我的應用程序中執行一項功能,該功能應該在其中之一的狀態更改時更新我的色相燈的狀態。
現在,我已經這樣做了:
async.waterfall([
function getLightsId(callback) {
args = {
path: {
"username": "webjeremie"
}
};
client.registerMethod("getLightState", "http://hue.jeremieledieu.fr/api/${username}/lights/", "GET");
client.methods.getLightState(args, function (data, response) {
var ids = [];
for (key in data) {
ids.push(key);
}
callback(null, ids);
});
},
function getLightsState(ids, callback) {
var lightsState = new Object();
async.reduce(ids, {}, function(lightsState, id, rcallback) {
getLightState(id, function(state) {
lightsState[id] = state;
rcallback(null, lightsState);
});
}, callback);
},
], function (err, currentState) {
io.sockets.on('connection', function (socket) {
socket.emit('updateLightsState', currentState);
previousState = currentState;
});
});
這讓我獲得了currenState
中所有燈的狀態,但是我陷入了困境:我應該如何不斷地將以前的狀態與當前狀態進行比較,並且僅當currentState!= previousState時才發光
有任何想法嗎 ?
我可以給你一個建議。
將值設置為變量。
var origVal = "Original";
var yourVariable = "Original";
setInterval(function(){
if(yourVariable !== origVal){
// do something.
}
},1000)
我認為, object.watch將在這里完成工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.