Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

end a setTimeout function before its set time

i have a jquery function that when clicked produces a set timeout on making a div visible.

however, if another option is selected during the settimeout length, i would like to know how to destroy this function and stoop anything else in it happening.

my current code is:

$(document).ready(function () {

    $('li#contact').click(function () {
        $('ul.image_display').css('display', 'none');
        $('ul.projects').fadeOut().hide();
        $('li#cv').removeClass('cur');
        $('li#projects').removeClass('cur');
        $('li#contact').addClass('cur');
        $('ul.contact').fadeIn(function () {
            setTimeout(function () {
                $('ul.contact').fadeOut('slow');
            }, 8000);
        });
        setTimeout(function () {
            $('li#contact').removeClass('cur');
            $('li#cv').addClass('cur');
            $('ul.projects').fadeIn('slow');
            $('ul.image_display').css('display', 'block');
        }, 8625);

    });

}); 

a bit cumbersome but works until this is clicked:

$(document).ready(function () {

    $('#projects').click(function () {
        $('li#cv').removeClass('cur');
        $('ul.contact').fadeOut().hide();
        $('#contact').removeClass('cur');
        $('ul.projects').fadeIn('slow');
        $('#projects').addClass('cur');
        $('ul.image_display').css('display', 'block');
    });

});

if the second is clicked just after the first than class 'cur' still comes up on li#cv after the set time.

like image 597
DanC Avatar asked Jun 02 '09 16:06

DanC


People also ask

How do I cancel setTimeout early?

To cancel a setTimeout() method from running, you need to use the clearTimeout() method, passing the ID value returned when you call the setTimeout() method.

How do I stop setTimeout interval?

Answer: Use the clearInterval() Method The setInterval() method returns an interval ID which uniquely identifies the interval. You can pass this interval ID to the global clearInterval() method to cancel or stop setInterval() call.

How do you delete a timeout in a function?

The clearTimeout() function in javascript clears the timeout which has been set by setTimeout()function before that. setTimeout() function takes two parameters. First a function to be executed and second after how much time (in ms). setTimeout() executes the passed function after given time.

What happens if setTimeout is not cleared?

In other words, it's a no-op; nothing happens, and no error will be thrown. Save this answer. Show activity on this post. You don't actually need to use clearTimeout , you only use it if you wish to cancel the timeout you already set before it happens.


2 Answers

The setTimeout function returns an identifier to that timeout. You can then cancel that timeout with the clearTimeout function. So you can do something like this (fill in the blanks with your code):

var timer;
$(function() {
    $(...).click(function() {
        ...
        timer = setTimeout(...);
        ...
    });

    $(...).click(function() {
        clearTimeout(timer);
    });
});

It's not particularly super clean to keep a global variable for this, however. You could store the timer in the data attribute of whatever element makes the most sense for your situation. Something like this:

$(function() {
    $(...).click(function() {
        ...
        var timer = setTimeout(...);
        $(someelement).data('activetimer', timer);
        ...
    });

    $(...).click(function() {
        var timer = $(someelement).data('activetimer');
        if(timer) {
            clearTimeout(timer);
            $(someelement).removeData('activetimer');
        }
    });
});

It doesn't really look cleaner, but it's an alternative way to store the timer...

like image 90
Paolo Bergantino Avatar answered Oct 28 '22 10:10

Paolo Bergantino


You can use clearTimeout() to do that. You'll need to keep the return value from setTimeout() in a variable to pass to clearTimeout().

like image 40
chaos Avatar answered Oct 28 '22 08:10

chaos