jquery - Start interval through timeout -


i have piece of code starts interval. push of button, want interval stopped/cleared, wait x seconds, , start interval again.

this code starts interval , works fine:

var timeout = undefined; var interval = setinterval(bgscroll, timebetweenswap); 

then want interval stopped when click button, let wait x seconds , start again (ofcourse pressing button again should reset amount of seconds)

$(document).on('click', '.bgscroller a', function(){     clearinterval(interval);     cleartimeout(timeout);      var timeout = settimeout(function(){         var interval = setinterval(bgscroll, timebetweenswap);     }, delayafterclick); }); 

now when click button few times interval goes crazy , stacks up. suggest cleared... advice oh how should fix this?

kind regards, narayan

in short, remove last 2 var statements. make timeout = , interval = instead of var timeout = , var interval =.

your code close—it looks you're shadowing timeout , interval variables on last few lines.

this bit that's wrong:

var timeout = settimeout(function(){     var interval = setinterval(bgscroll, timebetweenswap); }, delayafterclick); 

it should (very similar):

timeout = settimeout(function(){     interval = setinterval(bgscroll, timebetweenswap); }, delayafterclick); 

note removed var statements. uses same timeout , interval variables above, rather making new variables happen have same names.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -