Prevent js alert () from pausing timers

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

javascript: alert("blah");

in the address, a popup warning window pauses my timer. Which is very undesirable in the quiz.

I don’t think there is a way to stop this behavior ... but I will ask anyway.

If not, think what should I do?

+4
source share
8 answers

The rendering of the preview seems to be different from the published rendering. This paragraph is here to ensure that the next two lines are displayed as code.

// Preserve native alert() if you need it for something special
window.nativeAlert = window.alert;

window.alert = function(msg) {
    // Do something with msg here. I always write mine to console.log,
    // but then I have rarely found a use for a real modal dialog,
    // and most can be handled by the browser (like window.onbeforeunload).
};
+6
source

, javascript ( ), , .

, :

  • ,
  • ping javascript N , ( )
  • , , , (.. ).
+10

, , JavaScript. , , , .

+5

. , , Lightbox.

+3

, , , . javascript: alert ( "paused" ); , .

, , Date(), , , . , , . , .

, , . Date() javascript, .

. . , , . !

+3

SyaZ .

:

:

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);
+2

. , , , .

, , JavaScript, "", -, () , t window.alert:

var timer = {
    startDatetime: null,
    startSec: 0,
    variance: 1,
    exitOnPause: true,
    count: function (config) {
        var that = this;

        if (typeof config == "object" && typeof parseInt(config.seconds) == "number" && !isNaN(parseInt(config.seconds)))
        {
            if (typeof parseFloat(config.variance) == "number" && !isNaN(parseFloat(config.variance))) this.variance = config.variance;
            if (typeof config.exitOnPause == "boolean") this.exitOnPause = config.exitOnPause;

            if (config.seconds > 0)
            {
                if (!this.startSec) this.startSec = config.seconds;
                if (!this.startDatetime) this.startDatetime = new Date();
                var currentDatetime = new Date();

                if (currentDatetime.getTime() - this.startDatetime.getTime() > (this.startSec - config.seconds) * this.variance * 1000)
                {
                    if (typeof config.onPause == "function") config.onPause();

                    if (!this.exitOnPause)
                    {
                        this.startDatetime = new Date();
                        this.startSec = config.seconds--;
                        window.setTimeout(function () { that.count(config); }, 1000);
                    }
                }
                else
                {
                    config.seconds--;
                    window.setTimeout(function () { that.count(config); }, 1000);
                }
            }
            else
            {
                if (typeof config.onFinish == "function") config.onFinish();
            }
        }
    }
};

, count(), . , .

- window.setTimeout . , , window.setTimeout(x, 1000), 1 , 2 . , , , , . 1, . , 2,5 " " pokes:

timer.count({
    seconds: 10,
    onPause: function () { alert("You cheated!"); window.location.replace("cheatersAreBad.html"); },
    onFinish: function () { alert("Time up!"); },
    variance: 2.5
});

Ajax, script, , , . - , , exitOnPause false:

timer.count({
    seconds: 10,
    exitOnPause: false,
    onPause: function () { recordCheaterViaAjax(); },
    onFinish: function () { alert("Time up!"); },
    variance: 2.5
});
+2

1 . Javascript , , . , , , , .

0

All Articles