简体   繁体   中英

Countdown server side best practices

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

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(ba); },0); }
 <input onclick="startCountdown()" type="submit"> <p class="text" ></p>


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM