How to kill or cancel or remove setTimeout with clearTimeout?

Home Forums Javascript and jQuery Snippets How to kill or cancel or remove setTimeout with clearTimeout?

This topic contains 0 replies, has 1 voice, and was last updated by  sravkum 2 years, 2 months ago.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #4023

    sravkum
    Keymaster

    We generally do setTimeout as

    setTimeout("SomeFunction();", 5000); // Means setTimeout is set to 5 seconds

    Some defined or custom function in our case it is SomeFunction() which will run after 5 seconds.

    But what if you want to cancel or clear or avoid setTimeout after few seconds and before the function SomeFunction() is initiated?

    In this condition we need to use clearTimeout function.

    Generally to cancel or clear some timeout stuff – the best way to do is the following

    1. Put setTimeout in some variable, something like this
    var timer = setTimeout("SomeFunction();", 5000);

    2. Then now to cancel or clear it, do the following
    clearTimeout(timer);

    This is simple but very useful method in Javascript

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

Ignite Your Thoughts