2016-08-22 79 views
0

我創建了在線考試系統。javascript - 倒數計時器啓動和停止

我的問題:我點擊申請按鈕比倒計時器開始和有時瀏覽器頁面關閉後,我點擊相同的考試名稱,然後倒數計時器繼續,不開始開始。下面的截圖

screenshot-1

screenshot -2

查看:

<h2><p style="float: right" id="countdown"></p></h2> 

<a href="<?php echo base_url(); ?>student/Examinations/apply_examinations/<?php echo $value['examination_test_id']; ?>" class="btn btn-primary" id="apply" onclick="resetCountdownInLocalStorage();">Apply</a> 

    <script> 
     $(document).ready(function() { 
    $examination_test_id = $("#examination_test_id").val(); 
      $time_limit = $("#time_limit").val(); 
      var d = new Date($time_limit); 
      var hours = d.getHours(); 
      var minute = d.getMinutes(); 
      var minutes = hours * 60 + minute; 
      var seconds = 60 * minutes; 

      if (typeof (Storage) !== "undefined") { 
       if (sessionStorage.seconds) { 
        seconds = sessionStorage.seconds; 
       } 
      } 
      function secondPassed() { 
       var hours = Math.floor(seconds/3600); 
       var minutes = Math.floor((seconds - (hours * 3600))/60); 
       // var remainingSeconds = seconds - (hours * 3600) - (minutes * 60); 
       var remainingSeconds = seconds % 60; 

       if (remainingSeconds < 10) { 
        remainingSeconds = "0" + remainingSeconds; 
       } 
       if (minutes < 10) { 
        minutes = "0" + minutes; 
       } 

       if (typeof (Storage) !== "undefined") { 
        sessionStorage.setItem("seconds", seconds); 
       } 

       document.getElementById('countdown').innerHTML = hours + ":" + minutes + ":" + remainingSeconds; 

       if (seconds == 0) { 
        clearInterval(myVar); 
        document.getElementById('countdown').innerHTML = alert('Timeout'); 
        window.location.href = base_url + "student/Examinations/check_answer/" + $examination_test_id; 

        if (typeof (Storage) !== "undefined") { 
         sessionStorage.removeItem("seconds"); 
        } 
       } else { 
        seconds--; 
       } 
      } 
      var myVar = setInterval(secondPassed, 1000); 
     }); 

function resetCountdownInLocalStorage() { 
     sessionStorage.removeItem("seconds"); 
    } 
    </script> 
+0

我目前的形式並不完全瞭解你的問題。但是當你關閉瀏覽器並重新打開它時,你的'狀態'就會丟失。您正在有效地運行整個程序。如果您想在重新打開該頁面後「繼續」,則需要將該信息存儲在某處 –

+0

@DylanMeeus請編寫代碼 – kishan123

+0

@DylanMeeus首先查看屏幕截圖,然後單擊應用按鈕,然後定時器啓動並在某個時間之後關閉瀏覽器頁面幾分鐘我點擊相同的考試名稱申請然後倒數計時器開始繼續,不開始求助 – kishan123

回答

0

它可能會依賴於瀏覽器,根據該link

瀏覽的壽命上下文可能與實際用戶代理進程本身的生存期無關,因爲用戶代理可能在重新啓動後支持 恢復會話。

您可能必須清理選項卡上的會話stroage close我認爲。

+0

我點擊應用按鈕,所以應用按鈕火災onclick事件,這個事件在第二會刪除sessionstorage – kishan123

+0

首先看截圖,然後點擊應用按鈕,然後計時器開始和某個時候瀏覽器頁面關閉,幾分鐘後,我點擊相同的考試名稱申請然後倒數計時器開始繼續,不開始求生 – kishan123

+0

很難幫助你沒有更多的細節。你是否點擊應用瀏覽?你留在同一頁?你打開一個新標籤? – Mathieu