setTimeout(helper.setPinSync(0,true), 500);
will set the pin immediately. The first argument of setTimeout should be a function, but you're actually just passing in the return value of setPinSync
(which is undefined
). You probably meant to do this:
setTimeout(function() {
helper.setPinSync(0, true);
}, 500);
BUT, doing that will still just delay that call by 0.5s. If you do that a bunch of times in an infinite while loop, you're actually just queueing an infinite amount of timers to run, but since the loop never ends, the timers never get a chance to run. An infinite while/for loop will halt all timers, due to the way V8's event loop works.
Instead you probably want to use setInterval
, not in a loop:
var lightState = false;
setInterval(function() {
// Toggle
lightState = !lightState;
// Set value
helper.setPinSync(0, lightState);
}, 500);
Or you could recursively call setTimeout
. This one will toggle the light on/off at random intervals:
var lightState = false;
// Function which is triggered every 0+ to 1 seconds
var next = function() {
lightState = !lightState;
helper.setPinSync(0, lightState);
setTimeout(next, Math.random() * 1000);
}
next();
I also put some samples on GitHub