Run code in background while warning is displayed

This is my fragment.

$(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>
Run codeHide result

Just try reloading the page. You will receive a warning. During this warning, my timers do not work. In this way, the user can trick the test. How to solve this problem?

+4
source share
3 answers

I'm afraid that how it works alert. It blocks one thread of JavaScript running, waiting for input. I suggest warning in another way. Not only does this not block, but you can also style the real window with CSS.

+3
source

Javascript - , , "". javascript , , "".

, HTML CSS.

, , , javascript, . websockets .

+2

JavaScript (*), , script, - , , .

0

Source: https://habr.com/ru/post/1629909/


All Articles