You are reading a single comment by @CanyonCasa and its replies. Click here to read the full conversation.
  • Thanks @allObjects. Your first example will work for my application, cleaner than my workaround. I've seen this signature before, but haven't needed it for NodeJS, where I do most of my coding these days.
    Still, I don't see the value in throwing an error from clearTimeout where undefined could be gracefully returned instead, which doesn't require the user to know to clear the ID in the callback, or think of clearTimeout differently than clearInterval or differently than NodeJs code.
    FYI, I think your second example has a couple typos ...

    var timeout1 = {}, timeout2 = {};
    function timedFunction(timeout) {
      timeout.id = null;
      // ...other code...
    };
    // ...more code...
    timeout1.id = setTimeout(timedFunction, 1000, timeout1);
    timeout2.id = setTimeout(timedFunction, 2000, timeout2);
    // ...some code...
    if (timeout1.id) { timeout1.id = clearTimeout(timeout1.id); }
    // ...some code again...
    if (timeout2.id) { timeout2.id = clearTimeout(timeout2.id); }
    

    And similarly, I believe you meant line 11 of the last example to reference timeout2.

About

Avatar for CanyonCasa @CanyonCasa started