javascript - Set time out still runs when i clear the div -
i have settimeout have assigned div this:
function update(div,data){ div._mytimer = settimeout(function() { update(data);},1); }
but reason when remove div page time out still runs.
for example lets clear parent of div
doing:
parent.innerhtml = '';
shouldn't cancel time out because div no longer appears in dom? or have misunderstood something.
the timeout once set not need div exist. should remember reference timeout , clear manually before clear div.
when remove div setting innerhtml of parent, can every div going removed , check if has _mytimer property set , in case clear timer.
for (var i=0, ilen=parent.childnodes.length; i<ilen; i++) { if (parent.childnodes[i]._mytimer) { cleartimeout(parent.childnodes[i]._mytimer); } }
or if delete div way, can acces reference directly:
cleartimeout(div._mytimer);
div.parentnode.removechild(div);
not innerhtml = '', way have more control on elements removed
Comments
Post a Comment