4

This is my snippet.

$(function() {
  setCountDown(0, 1, 0);
  a = Array();
  $(window).bind('beforeunload', function() {
    if (a['endTime']) {
      return ("You are in the middle of a test");
    }
  });
});

function setCountDown(hours, minutes, seconds) {
  $(".timer").removeClass("common_displayNone");

  timerInterval = setInterval(
    function() {
      if (hours.toString().length == 1) {
        hours = "0" + hours;
      }
      if (minutes.toString().length == 1) {
        minutes = "0" + minutes;
      }
      if (seconds.toString().length == 1) {
        seconds = "0" + seconds;
      }

      if (seconds > 0) {
        seconds--;
        if (seconds.toString().length == 1) {
          seconds = "0" + seconds;
        }
      } else if (seconds == 0 && (minutes > 0)) {
        minutes = minutes - 1;
        seconds = 59;
      } else if (minutes == 0 && (hours > 0)) {
        hours = hours - 1;
        minutes = 59;
      }

      if (hours == 0 && minutes == 0 && seconds == 0) {
        clearInterval(timerInterval);
        a.removeItem("endTime");
      }

      $("#timer").html(hours + ":" + minutes + ":" + seconds);
      a['endTime'] = (hours * 3600 + minutes * 60 + seconds);
    }, 1000);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="timer"></div>

Just try to reload the page. You will get an alert. During that alert, my timers are not running. So user can cheat test. How to solve this issue?

Tushar
  • 85,780
  • 21
  • 159
  • 179
Gibbs
  • 21,904
  • 13
  • 74
  • 138
  • 7
    Javascript is single-threaded and `alert`s are blocking. In other words, **this cannot be done** – Tushar Feb 22 '16 at 15:04
  • You **cannot** do this as [Javascript is single-threaded](http://stackoverflow.com/questions/2734025/is-javascript-guaranteed-to-be-single-threaded) – solimanware Feb 22 '16 at 15:08
  • 1
    You could store the time the alert was shown, and then when it is closed adjust the timer based on the time elapsed... Or use a modal dialog instead of an alert. – Jim Edelstein Feb 22 '16 at 15:09

3 Answers3

4

I'm afraid that's just how alert works. It's blocking the single thread JavaScript runs on, waiting for an input. I'd suggest alerting in some other fashion. Not only would it be non-blocking, but you could also stylize the actual alert box with CSS.

GMchris
  • 5,439
  • 4
  • 22
  • 40
2

Javascript is a single threaded language, which means that you cannot have code running on the "background". When javascript prompts an alert window, it waits for the user to click "Ok".

If you really want to have popups, you'll have to do it with HTML and CSS.

I'd recommend handling the time a user has for his test server side if you're worried about cheating, because a user can still edit your javascript allowing himself to take more time. You could accomplish this with websockets or polling.

Glubus
  • 2,819
  • 1
  • 12
  • 26
0

JavaScript is usually considered to have a single thread of execution visible to scripts(*), so that when your inline script, event listener or timeout is entered, you remain completely in control until you return from the end of your block or function.

solimanware
  • 2,952
  • 4
  • 20
  • 40