return $r; } /** * @param int $page 页数 * @param int $pagesize 每页显示数量 * @return mixed */ function link_find($page = 1, $pagesize = 100) { $arr = link__find($cond = array(), array('rank' => -1), $page, $pagesize); return $arr; } /** * @param $id * @return bool 返回FALSE失败 TRUE成功 */ function link_delete($id) { if (empty($id)) return FALSE; $r = link__delete(array('id' => $id)); link_delete_cache(); return $r; } //--------------------------kv + cache-------------------------- /** * @return mixed 返回全部友情链接 */ function link_get($page = 1, $pagesize = 100) { $g_link = website_get('friends_link'); if (empty($g_link)) { $g_link = link_find($page, $pagesize); $g_link AND website_set('friends_link', $g_link); } return $g_link; } // delete kv and cache function link_delete_cache() { website_set('friends_link', ''); return TRUE; } ?>javascript - setInterval stop working on alert - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - setInterval stop working on alert - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 5

Browser 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.

发布评论

评论列表(0)

  1. 暂无评论