You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • Good practice - I use - is:

    • define (global) variables for all your watches, timeouts, and in intervals you plan to set
    • give the name a suffix, like xyzWId or xyzWHndl for watch handles
    • initialized them with false at declaration (optional, any falsy evaluating value will do: undefined, null, 0, "", false)
    • always use the variable when setting the Watch / Interval / Timeout
    • always use the following construct when clearing a Watch / Interval / Timeout - mostly I use a function or method of an object where I have a simplified invocation interface to make sure I do not misspell the variable name and other things. Most of the time there is more that has to go on at the same time with the particular clear...(), and that goes nicely in such a function or method.... and some times these things have even to go conditioned on the ...Id as well.

      var xgzWId = false;
      // ...
      // ...
      // ...
      xyzWid = setWatch(xyzFunction,pin,options);
      // ...
      // ...
      // ...
      if (xyzWId) { xyzWId = clearWatch(xyzWId); }
      

    PS: clear...() returns undefined, which is falsy and does the expected job.

About

Avatar for allObjects @allObjects started