javascript - Clearing setInterval() Issue -


i have setinterval on function x runs every 500ms. in function x, call function y binds event on divs. however, unbind these events next time function x called (to start "fresh"). code doesn't seem work:

setinterval(this.board.updateboard, 500); //called constructor 

this initiates functions below:

board.prototype.updateboard = function() {     //i attempt unbind divs     var divs = this.$el.find("div");     for(var = 0; < divs.length; i++) {         $(divs[i]).unbind(); //apparently doesn't work?     }      //...some code here...     //find appropriate $div's (multiple of them), , calls this.beginwalking() below on each of      //loop here     this.beginwalking($div, direction + "0", direction + "1");     //end of loop }  //alternate between classes give appearance of walking board.prototype.beginwalking = function ($div, dir0, dir1) {      return setinterval(function () {         if ($div.hasclass(dir0)) {             $div.removeclass(dir0);             $div.addclass(dir1);         } else {             $div.removeclass(dir1);             $div.addclass(dir0);                     }     }.bind(this), 80); }; 

basically, updateboard called every 500ms. each time it's called, beginwalking called set interval on div. purpose of other interval, functions correctly, add , remove class every 80ms. can't seem unbind before next updateboard called.

any suggestions appreciated!

use clearinterval() edit: $(selector).toggleclass(dir0) might helpful

// in other file, use global (no var) if need read file: updaterglobal = setinterval(this.board.updateboard, 500);  // store interval references clearing: var updaterlocals = []; board.prototype.updateboard = function() {     //i attempt unbind divs     var divs = this.$el.find("div");     // stop existing div timers:     while(updaterlocals.length > 0){       clearinterval(updaterlocals[0]);       updaterlocals.shift(); // remove first timer     }      //...some code here...     //loop here call below on several $div's     this.beginwalking($div, direction + "0", direction + "1");     //end of loop }  //alternate between classes give appearance of walking board.prototype.beginwalking = function ($div, dir0, dir1) {      var interval = setinterval(function () {         if ($div.hasclass(dir0)) {             $div.removeclass(dir0);             $div.addclass(dir1);         } else {             $div.removeclass(dir1);             $div.addclass(dir0);                     }     }.bind(this), 80);     // save timer:     updaterlocals.push(interval);     return; }; 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -