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  Sravan Kumar 5 years, 11 months ago.

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

    Sravan Kumar

    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

    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

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

Send this to a friend