Can a non repetitive timer be interrupted before it expires

I call a makeWebRequest function but just before the call, I start a non repetitive timer than will display a message if the makeWebRequest call takes longer than expected to execute. I would like to abort that timer if the makeWebRequest call returns before the timer expires, but timer.stop doesn't do that. I'm thinking of using a variable in the timer's callback but find that solution not elegant. Is there a way to abort a timer so its callback is never called?

  • Not sure how you can stop the timer, but I don't think you need it.  If the makeWebRequest takes too long, you'll get an error code in the callback  (-2 for example)

  • Hi, the thing is makeWebRequest can take up to 10 seconds on my watch before timing out. I want to display a warning after 5 seconds that the app is still waiting for data but if it gets that data before that 5 seconds is up, I want to cancel the timer. Right now I set a variable when data is received and when the timer expires, if that variable is set, I don't display that message. Canceling the timer would be more elegant but I doubt it's possible,

  • It's not a timer. Once the timer triggered you started a web request. Now what you want to cancel is not the timer but the web request.

    So either you find how to do that or try a workaround:

    When you start the we request start a new timer for 5 seconds that calls a callback and set a boolean. In the web request callback unset the boolean. So when the new timer calls your (other) callback you can check the boolean. If it's set you display the message about timeout, if it's not set, you ignore it.

    Similarly in the web callback you might also ignore if you detect that you already displayed the timeout message

  • Hi. it's close to what I'm doing. Just before calling the web request, I set the boolean and I start the timer.The boolean is cleared in the web request method (ie, when something is returned). If the boolean is still set when the timer ends, I display the message on screen.

    It would have been 'cleaner' though if I could have aborted the timer within the web request method, but it's working though so that's what matter :-) Thanks