Prevent js alert() from pausing timers

后端 未结 8 933
花落未央
花落未央 2021-01-06 01:54

So I made some timers for a quiz. The thing is, I just realized when I put

javascript: alert(\"blah\");

in the address, the popup alert bo

8条回答
  •  灰色年华
    2021-01-06 02:13

    The feedback loop on SyaZ's question has clarified the issues at stake.

    Here's an attempt to summarize the good answers so far:

    • Client scripts are by nature are easy to manipulate to cheat an online quiz. SEE @Filini 's Server-side approach

    • window.alert = function(msg) {} will overriding alert() and perhaps defeat the low hanging fruit of putting in the addressbar: javascript:alert('Pausing page so I can google the answer') or I'll use my Phone-A-Friend now. Courtesy of @eyelidlessness

    • If you must use a client-side approach, instead of using setTimeOut(), you could use a custom date-compare-based pause function like this (concept by @Mnebuerquo, code example by me (@micahwittman)):

    Example:

    var beginDate = new Date();
    
    function myTimeout(milsecs){
      do { curDate = new Date(); }
        while((curDate-beginDate) < milsecs);
    }
    
    function putDownYourPencils(milsecs){
      myTimeout(milsecs);
      var seconds = milsecs / 1000;
      alert('Your '  + seconds + ' seconds are up. Quiz is over.');
    }
    
    putDownYourPencils(3000);
    

提交回复
热议问题