Jquery kill un-needed timeouts -


so have 2 buttons.

button a: button. (#button1)

button b: fake submit button. (#right_r)

button c: submit button. (#right)

by default, button show, , preventdefault() on click , show error message.

when click on button a, set timeout 8000, in 8 seconds, replace button b button c.

but problem is:

when user clicks on button multiple times, sets lot of timeouts.

what want is, kill previous timeout before setting new one, like stop it.

my code:

$(document).ready(function() {     $("#right_r").click(function(event) {         event.preventdefault();         $("#error").slidedown("slow");          settimeout(function() {             $("#error").slideup("slow");             }, 1000);     });     $("#button1").click(function() {         settimeout(function() {             $("#right_r").hide();             $("#right").show();         }, 8000);     }); }); 

thanks.

try this

var right_r_timeout = null, runelocus_timeout = null; $(document).ready(function() {     $("#right_r").click(function(event) {         event.preventdefault();         $("#error").slidedown("slow");          if (right_r_timeout != null) {             cleartimeout(right_r_timeout);             right_r_timeout = null;         }         right_r_timeout = settimeout(function() {             $("#error").slideup("slow");         }, 1000);     });      $("#runelocus").click(function() {         if (runelocus_timeout == null) {             cleartimeout(runelocus_timeout);             runelocus_timeout = null;         }         runelocus_timeout = settimeout(function() {             $("#right_r").hide();             $("#right").show();             runelocus_timeout = null;         }, 8000);     }); }); 

see https://developer.mozilla.org/en/docs/dom/window.settimeout


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -