CommandFusion / iViewer-For-Android

CommandFusion iViewer for Android
2 stars 0 forks source link

setInterval function problem #118

Closed NhaThongMinh closed 11 years ago

NhaThongMinh commented 11 years ago

I have js code to send OnCommand Relay each 4s and OffCommand Relay each 8s:

setInterval(function(){CF.runCommand(null,"Relay-1-40-1-100");},4000); setInterval(function(){CF.runCommand(null,"Relay-1-40-1-0");},8000);

but iViewer send them almost at the same time. (my light on and off instantly). Run on Android 2.3.6 , iViewer beta 140.

fpillet commented 11 years ago

Well ... as the going say, it's a user error. You are scheduling ON every 4 seconds, and OFF every 8 second. So guess what will happen?

(4s) ON (8s) ON OFF (12s) ON (16s) ON OFF etc etc

I'm not sure what the intended effect is, but I think you need to revise your logic. I say that what you want to do is something like:

var lightsOn = false; // lights start in OFF state
flipLights();  // initially turn lights ON
setInterval(flipLights, 8000);

function flipLights() {
  lightsOn = !lightsOn;  // invert status
  if (lightsOn) {
     // turn lights on
    CF.runCommand(null, "Relay-1-40-1-100");
  } else {
    // turn lights off
    CF.runCommand(null, "Relay-1-40-1-0");
  }
}