javascript - Reset timer on click for show and hide - jquery -
i have jquery task on click of button, have display text message 10 seconds i've implemented.
now problem while text still visible, if click on same button, should reset timer (the timer should 0 , text should show 10 seconds again). below did. can tell me i'm doing wrong?
$('#mybtn').live('click', function(e){ /*mybtn button mytext text should appear */ e.preventdefault(); var $mytext = $('#mytext'); if( $mytext.length >0){ //text existing or showing $mytext.stop(); $mytext.show().delay(10000).hide(500); } else{ $mytext.show().delay(10000).hide(500); } });
try this:
var timer; $('#mybtn').on('click', function(e){ var $mytext = $('#mytext'); $mytext.show(500); cleartimeout(timer); timer = settimeout(function(){ $mytext.hide(500) },10000); });
working fiddle here: http://jsfiddle.net/jukbh/
check fiddle: http://jsfiddle.net/jukbh/2/
Comments
Post a Comment