javascript - stopping dynamically generated setInterval -
i generating multiple charts each own setinterval refresh data. have set clearinterval when dynamically generated container removed - if reload , has same id old setinterval continues run. there way set dynamically named setinterval can stopped when replacement generated?
right i'm using:
function generatechart(data, location){ var chart = new highcharts.chart({ // blah blah blah }, function(chart){ setinterval(function(){ if($('#'+location).length){ // i'm doing stuff every minute }else{ clearinterval(); } },60000); }); }
what happens is, location randomly generated string becomes element id container highchart , if user saves chart becomes unique identifier. if user updates chart that's saved , reloads chart, old 1 gets .removed() , new 1 generated in place. new 1 has same element id old 1 , since old interval finds container wants attempts continue updating - can't since chart went poof.
is there way set dynamic variable can use setinterval can clearinterval on it?
var blob+location = setinterval(function(){ ...
and then
clearinterval(blob+location);
you can use object:
var myobj = {}; var location = "somevalue"; myobj[location] = setinterval(... clearinterval(myobj[location]);
Comments
Post a Comment