NEWS
[gelöst] clearTimeout -> Prüfen ob Timeout gesetzt ist?
-
Wie kann ich prüfen ob ein Timeot gesetzt ist?
Ich habe:
var timeoutVar=false; var power=0; on("hm-rpc.POWER", function (obj) { if(power<10){ if(!timeoutVar){ timeoutVar=setTimeout(function(){console.log("warte");},50000); } }else{ clearTimeout(timeoutVar); } } console.log(timeoutVar);
Mein Problem ist, auch nach clearTimeout ist console.log(timeoutVar); ein [object Object]
was mache ich falsch? Ich dachte mit clearTimeout wird das dann null oder wenigstens false.
-
Timervariablen sind Objekte. Deshalb sollte man sie so anwenden:
var timeoutVar = null; on("hm-rpc.POWER", function (obj) { if(obj.state.val < 10){ timeoutVar = setTimeout(function(){ console.log("Zeit abgelaufen"); timeoutVar = null; }, 50000); } else if(timeoutVar) { clearTimeout(timeoutVar); timeoutVar = null; } if(timeoutVar) console.log('Timer läuft'); });
-
Ah ok, dann setze ich das immer auf null.
Hatte in der Docu geschaut und da war das Beispiel immer ohne auf null setzen.