1

Problem
I have a countdown that is server side and needs to be shown on the client. First i made it using socket.io basically emitting it every 100 milliseconds. Now i really like this but i fear there might be significant performance downgrade to instead just emit a signal and having the client make the countdown but this brings another problem which is not being able to see the countdown value until it resets. Is emitting every 100ms really that bad of a practice or is this ok ?

Edit
I need it server side because of authentication and consistency for all users loop is small 40 secs followed by a 10 sec pause. I need to know if users have performed the action in that period

Edi_Hadzic
  • 133
  • 10
  • What do you mean by "not being able to see the countdown value until it resets"? – Sal Dec 21 '20 at 19:19
  • Well if i send a signal only one emit instead of thousands to user when the counter starts again. Users who join while counter is running would not receive the signal from my server and client js would not start the timer for them. Meaning they have to wait for the counter to restart and send signal again – Edi_Hadzic Dec 21 '20 at 19:27
  • It's very difficult to know what is best here without understanding the use case. What motivated you to run the countdown away from the client in the first place? What problem does running it server-side seek to solve? – spender Dec 21 '20 at 19:35
  • "because of authentication"? It is still not clear what problem you are trying to solve. – spender Dec 21 '20 at 19:38
  • If you are looking to keep the time synchronized between all of your clients, what about storing the start time (in UTC for consistency maybe) and countdown duration in your server, then returning that data through an API call? This way, you're not emitting potentially thousands of times or more every few minutes, and you can still have a timer on the front-end with Fus_ion's implementation below. – Sal Dec 21 '20 at 19:43
  • The thing is i that timer needs to be very consistent and same for all users because it is in very small loop around 40 seconds i need to know if user action was performed before or after 40 seconds have expired followed by small pause. Doing this in client side may lead to differences between the server timer and client one. – Edi_Hadzic Dec 21 '20 at 19:56
  • Thank you @Sal seems like a good way to do this i will look into it – Edi_Hadzic Dec 21 '20 at 20:02
  • Just to note, it's possible that there would be a small discrepancy in each client in the way I outlined, though this should not be too large, depending on the client's internet connections, size of the data, etc. – Sal Dec 21 '20 at 20:05

1 Answers1

0

Honestly, I would recommend doing it by the client because it's easiest and better even if they had some type of disconnection from the server


function msToTime(duration) {
    var seconds = parseInt((duration/1000)%60),minutes = parseInt((duration/(1000*60))%60),hours = parseInt((duration/(1000*60*60))%24),days  = parseInt(duration/(1000*60*60*24));

    var hoursDays = parseInt(days*24);
    hours += hoursDays;
    hours = (hours < 10) ? "0" + hours : hours;
    minutes = (minutes < 10) ? "0" + minutes : minutes;
    seconds = (seconds < 10) ? "0" + seconds : seconds;
    return hours + ":" + minutes + ":" + seconds;
}

function startCountdown(){
setInterval(function(){
  var a = new Date().getTime();
  var now = new Date();
  var b = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 24, 0, 0, 0).getTime();
  document.getElementsByClassName("text")[0].innerHTML= msToTime(b-a);
  },0);
}
<input onclick="startCountdown()" type="submit">
<p class="text" ></p>

Function From here: https://stackoverflow.com/a/19700358/14746601

Fus_ion
  • 41
  • 6