I am creating a MCQs quiz and there is a time for each question. I have also used onbeforeunload
in my script to prevent the user from accidentally navigating away from the quiz page. But when that alert gets display my setInterval
counter stop working. How can i make the countdown to not stop even if there is an alert?
To further explain my problem here is the FIDDLE
I am creating a MCQs quiz and there is a time for each question. I have also used onbeforeunload
in my script to prevent the user from accidentally navigating away from the quiz page. But when that alert gets display my setInterval
counter stop working. How can i make the countdown to not stop even if there is an alert?
To further explain my problem here is the FIDDLE
Share Improve this question asked Apr 9, 2014 at 20:31 Khawer ZeshanKhawer Zeshan 9,6568 gold badges42 silver badges63 bronze badges 1- Javascript is single-threaded so that's not possible. But you can record the date() the alert appears and then the date() alert disappears. You might find some help with this question: stackoverflow./questions/12731865/… – Kivak Wolf Commented Apr 9, 2014 at 20:34
3 Answers
Reset to default 5Browser dialogs are blocking. All script execution stops when they are open. You cannot change this behaviour.
Instead of starting with 90 seconds and trying to reduce that by 1 second every second, calculate the current date + the 90 seconds to get the end date. Then on each call of your setTimeout
, calculate the current number of seconds until the end date.
The timer won't advance while the alert is visible, but it will skip to where it should be when the alert is dismissed.
A possible workaround could be that you declare a var timeCount = new Date()
to store the time if a beforeunload()
is called and use that to calculate passed time/missed seconds. But otherwise what Diodeus said is correct.