javascript - ClearInterval undefined when button clicked -


suppose have few buttons , when clicked trigger setinterval, when clicked on different button previous setinterval doesn't clear or undefined.

example:

$("#button1").click(function () {          var url = "xxx";         var min = "yyy";          getgraphcredentials3(min,url);          var onehour = setinterval(function () {                 getgraphcredentials3(min,url);         }, 5000);           clearinterval(twohour);    });   $("#button2").click(function () {          var url = "zzz";         var min = "uuu";          getgraphcredentials3(min,url);          var twohour = setinterval(function () {                 getgraphcredentials3(min,url);         }, 5000);           clearinterval(onehour);       }); 

can please?

much appreciated

the scope of onehour , twohour functions function in they're declared, they're not visible other callback.

you must declare variables in common scope. :

var onehour, twohour; $("#button1").click(function () {          var url = "xxx";         var min = "yyy";          getgraphcredentials3(min,url);          onehour = setinterval(function () {                 getgraphcredentials3(min,url);         }, 5000);           clearinterval(twohour);    });   $("#button2").click(function () {          var url = "zzz";         var min = "uuu";          getgraphcredentials3(min,url);          twohour = setinterval(function () {                 getgraphcredentials3(min,url);         }, 5000);           clearinterval(onehour);       }); 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -