JavaScript Using clearTimeout with multiple instances of setTimeout

I'm creating an 'advanced' Tic Tac Toe game in which each turn disappears after 10 seconds (ie: each X or O placed reverts to a blank square after 10 seconds). This all works fine, but the issue arises if a user decides to cancel the current game and start a new one.

If a user starts a new game and clicks on a square that was previously occupied, the timeout function will clear that square in accordance with the click from the previous game -- ie, in less than 10 seconds.

Using clearTimeout resets the timer for the most recent instance of SetTimeout, but doesn't help if there were multiple squares selected when the user reset the game board. And because setTimeout is applied to each X and O in an onclick function, I don't have a way to track multiple instance IDs.

Any thoughts would be much appreciated, code below.

EDIT: You can see a live version of the game (WIP) here: http://kylechadha.com/projects/tic-tac-toe/

Global variables:

var elements = document.getElementsByClassName('cell');
var rows = document.getElementsByClassName('row');
var alternate = 0;
var counter = 0;
var timerX; // Handles setTimeout instances for 'X'
var timerO; // Handles setTimeout instances for 'O'

Function to set X's and O's:

  var play = function() {
  for (i = 0; i < elements.length; i++) {
    elements[i].onclick = function () {
      if (this.className[0] == "c" && win == false) {
        if (alternate % 2 == 0) {
          this.className = "xmove";
          alternate++;
          counter++;
          var paramPass = this;
          timerX = setTimeout(function() {disappear(paramPass);}, 10000) // Handles ID of most recent instance of setTimeout for 'X'
        } else {
          this.className = "omove";
          alternate++;
          counter++;
          var paramPass = this;
          timerO = setTimeout(function() {disappear(paramPass);}, 10000) // Handles ID of most recent instance of setTimeout for 'O'
        }
      }
      position(this);
      analysis();
    }
  }
}

Reset function when user clicks 'New Game':

var reset = function() {
  header[0].innerHTML = "Tic Tac Toe";
  for (i = 0; i < rows.length; i++) {
    for (j = 1; j < 6; j += 2) {
      rows[i].childNodes[j].className = "cell animated bounceIn";
    }
  }
  clearTimeout(timerX); // Clears Timeout for most recent instance (last 'X' clicked) before the game was reset
  clearTimeout(timerO); // Clears Timeout for most recent instance (last 'O' clicked) before the game was reset
  board = [["","",""],["","",""],["","",""]];
  counter = 0;
  alternate = 0;
  win = false;
}
Answer:1

Keep a list of pending timeouts. Each timeout removes itself from the list when it triggers. When you reset, iterate over the list and clearTimeout on each.

Something like this:

var pending = {};
function mySetTimeout(callback, delay) {
  var t;
  t = setTimeout(function() {delete pending[t];callback()}, delay)
  pending[t]=1;
}
function clearAllTimeouts() {
  for (var t in pending) if (pending.hasOwnProperty(t)) {
    clearTimeout(t);
    delete pending[t];
  }
}
Answer:2

Ok, i spent all day to trying "connect" css styles with svg files created in inkscape. I've got svg file with map and couple of path, and i want to change fill color when i mouseover on that path. ...

Ok, i spent all day to trying "connect" css styles with svg files created in inkscape. I've got svg file with map and couple of path, and i want to change fill color when i mouseover on that path. ...

  1. change path fill color css
  2. change path fill css
  3. change fill path
  4. change svg path fill color
  5. wpf change path fill color
  6. change svg path fill css
  7. javascript change path fill color
  8. jquery change path fill

I am using an awesome html2canvas function but I have a noob question. How do I change the field it is capturing from document.body to a specific panel? In short i need to change document.body to the ...

I am using an awesome html2canvas function but I have a noob question. How do I change the field it is capturing from document.body to a specific panel? In short i need to change document.body to the ...

I cant find why my code isn't working. When I click on the generated element the alert is not firing. Here is a fiddle : http://jsfiddle.net/pZAdP/ And the code <button id="addMenuItem">Add ...

I cant find why my code isn't working. When I click on the generated element the alert is not firing. Here is a fiddle : http://jsfiddle.net/pZAdP/ And the code <button id="addMenuItem">Add ...

  1. jquery click event not firing
  2. jquery click event listener
  3. jquery click event target
  4. jquery click event firing twice
  5. jquery click event not working on mobile
  6. jquery click event get element
  7. jquery click event on class
  8. jquery click event function
  9. jquery click event stop propagation
  10. jquery click event parameters
  11. jquery click event only works once
  12. jquery click event get element id
  13. jquery click event for dynamic elements
  14. jquery click event on anchor tag
  15. jquery click event pass parameter
  16. jquery click event on pseudo element
  17. jquery click event on element
  18. jquery click event on disabled input
  19. jquery click event order
  20. jquery click event get href

I have a table and in one td I have an another table. What I am trying to do is select only the last tr from the first table (which is represented in the example below). My table HTML: <table&...

I have a table and in one td I have an another table. What I am trying to do is select only the last tr from the first table (which is represented in the example below). My table HTML: <table&...

  1. jquery select last element
  2. jquery select last child
  3. jquery select last option
  4. jquery select last row of table
  5. jquery select last of type
  6. jquery select last li in ul
  7. jquery select last element with class
  8. jquery select last option in dropdown
  9. jquery select last child of parent
  10. jquery select last element in array
  11. jquery select last two elements
  12. jquery select last child with class
  13. jquery select last div
  14. jquery select last of class
  15. jquery select last character of string
  16. jquery select last item
  17. jquery select last tr
  18. jquery select last td in tr