J
J
JaguXam2019-04-28 02:34:07
Node.js
JaguXam, 2019-04-28 02:34:07

How to make multiple independent timers per user in NodeJs?

The task is this.
On the site, people play with each other in different rooms. When someone joins another user in a room, a 10 second timer should start for that particular room.
Here is the code:

gameTimer1v1(gameData.gid, '10');
function gameTimer1v1(gameid, timer)
  {
               roundGTimer = timer;
    
    thisTimer = setInterval(function()
      {   
        roundGTimer--
        console.log(""+gameid+" - "+roundGTimer+"");
        
      if 	(roundGTimer == 0)
        {
          clearInterval(this);
          return false;
        }
        
      }, 1000);			
  }

If from the whole site the game is played only in one room, then the timer works correctly. It counts 10 seconds, but if, for example, the game starts in another room in parallel, the timer starts counting again.
It turns out that the function is not, as in php, executed for each action, but, as it were, layered on top of each other.
How can I make it so that each room has its own timer countdown?
I will be grateful for any answers.
Below is a screenshot from the console.
e5079d474e74ba5c0cfa3f911ad8298a.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Alekseev, 2019-04-28
@string2016

Add a field to the room when the event should occur and do a check on this field. As an option.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question