简体   繁体   English

如何避免用户添加断点来停止js中的倒数计时器?

[英]How to avoid user to add breakpoint to stop the count down timer in js?

I've been working on a online test system.User takes test in a limited time. 我一直在开发在线测试系统。用户在有限的时间内进行测试。 So I use js timer function. 因此,我使用js计时器功能。

 function timer(intDiff){
    window.setInterval(function(){

    var minute=0,
        second=0;       
    if(intDiff > 0){

        minute = Math.floor(intDiff / 60);
        second = Math.floor(intDiff) - (minute * 60);
    }
    if (minute <= 9) minute = '0' + minute;
    if (second <= 9) second = '0' + second;

    $('#minute_show').html('<s></s>'+minute+'minute');
    $('#second_show').html('<s></s>'+second+'second');
    intDiff--;
    }, 1000);
} 

$(function(){
    timer(intDiff);
}); 

but user can add breakpoint in chrome to make the time stop. 但是用户可以在chrome中添加断点以使时间停止。 How do I avoid user to do that.I know I can check the server time and client submit time.Is there any easy and better solution? 我该如何避免用户这样做。我知道我可以检查服务器时间和客户端提交时间。是否有任何简单且更好的解决方案?

Short answer, you can't. 简短的答案,你不能。

Long answer: 长答案:

You're design is bad, you should base your time measurement using the date object. 您的设计很糟糕,应该使用日期对象作为时间度量的基础。 Each time a loop will get triggered, check the current date with new Date() and keep track of the last date. 每次触发循环时,请使用new Date()检查当前日期并跟踪最后一个日期。

Keep in mind that even if you set an interval of 1000ms, it doesn't mean that on the function will get called every seconds. 请记住,即使您设置了1000ms的间隔,也并不意味着该函数将每秒钟被调用一次。 It only means that it will never get called before 1000ms. 这仅意味着它将永远不会在1000ms之前被调用。 As Javascript runs in one thread, any blocking process may delay calling methods that will remain in the event queue ( setInterval in this case). 由于Javascript在一个线程中运行,因此任何阻塞过程都可能会延迟将保留在事件队列中的调用方法(在这种情况下为setInterval )。

This way, it might not be necessary to track server time this way. 这样,可能不必以这种方式跟踪服务器时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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