From 9aed8171d028bbf904d06dae66502f759253ad41 Mon Sep 17 00:00:00 2001 From: gocivici Date: Tue, 26 Sep 2023 18:40:27 +0300 Subject: [PATCH] timer fix --- backend.js | 10 ++++++---- public/index.js | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/backend.js b/backend.js index f976b1a..bc64d66 100644 --- a/backend.js +++ b/backend.js @@ -31,7 +31,8 @@ io.on('connection', (socket)=>{ clickX:615, clickY:136, message:'', - username:"Visitor" + username:"Visitor", + timeoutId:0 } //create player object with new socket id property io.emit('updatePlayers',bPlayers) @@ -56,15 +57,16 @@ io.on('connection', (socket)=>{ }) socket.on('message',(message)=>{ - - clearTimeout(timeoutHandle); + let timeoutId; + clearTimeout(bPlayers[socket.id].timeoutId); bPlayers[socket.id].message = message function startTimer() { - timeoutHandle = setTimeout(() => { + timeoutId = setTimeout(() => { bPlayers[socket.id].message = '' }, 4000); } + bPlayers[socket.id].timeoutId = timeoutId startTimer(); console.log(bPlayers) diff --git a/public/index.js b/public/index.js index ea940bf..53d0f63 100644 --- a/public/index.js +++ b/public/index.js @@ -16,7 +16,7 @@ let row = 0; let readyNow = false let animationFrame = 0; let pageVisible = true; - +let timeoutHandle; // let atPosition = true; // img.src = 'demaSprite.png'; @@ -86,6 +86,7 @@ socket.on('updatePlayers', (bPlayers) =>{ fPlayers[id].clickY = bPlayer.clickY fPlayers[id].message = bPlayer.message fPlayers[id].username = bPlayer.username + //update player position without animation if tab is inactive if (document.hidden) { fPlayers[id].position.x = bPlayer.x