setTimeout, setInterval, setWatch logic

Posted on
  • Hi Gordon,

    Do i have to protect my function from calling twice in "parallel" ?
    Let say i have a function that talks over an uart to a device. The communcation must happen in series and never parallel.

    I call this function by setInterval/setTimeout and with setWatch. The protection is easy, i can set a variable inside the function at the beginning and clear it at the end ant checking for that.

    I just want to know if it can happen.

    Thanks

    Sacha

  • As i understand it the Espruino doesn't multitask all watches, intervals and timeouts happen one after another. I encounter this when i tried to do a watch on a button when i made an infinite loop, woops. Somthing like this wouldn't work.

    setWatch(function(){console.log("test");­}, BTN1, {repeat:true,edge:"rising", debounce:1/*ms*/});
    
    while(1){
    
    }
    
  • You don't have to to anything like that.

    watches/timeouts/intervals do not interrupt other code that is running when the timeout/interval happens or the watched pin changes - they get queued up, and run after the other code is done.

  • Many thanks, that's perfect.

    Sacha

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

setTimeout, setInterval, setWatch logic

Posted by Avatar for Sacha @Sacha

Actions