2012-06-08 23 views
2

我們對我們網站的在線問答部分,它有一個JavaScript代碼,算我們的測試人員參與一個特定的測試時間:本計數進位和倒數計時器在javascript

fiddle

526代碼是指從測驗通過秒數開始

document.writeln("<span id=\"time_online1\"></span>"); 
zi_inceput1 = new Date(); 
ceas_start1 = zi_inceput1.getTime(); 

function initStopwatch1() { 
    var timp_pe_pag1 = new Date(); 
    return ((timp_pe_pag1.getTime() + (1000 * 526) - ceas_start1)/1000); 
} 

var time = 5 * 60; 

function getSecs1() { 
    var tSecs1 = Math.round(initStopwatch1()); 
    var iSecs1 = time - tSecs1 % 60; 
    var tMins1 = Math.round(iSecs1/60); 
    var iMins1 = time - tMins1 % 60; 

    var sSecs1 = iSecs1 % 60; 
    var sMins1 = iMins1 % 60; 



    if (sMins1 === '0' && sSecs1 === '0') { 
     alert('time is up and you can not continue, the test is submited.'); 
     document.getElementById('time_online1').innerHTML = "times up"; 
     document.qs.submit(); 
    } else { 

     document.getElementById('time_online1').innerHTML = sMins1 + ":" + sSecs1; 
     window.setTimeout('getSecs1()', 1000); 

    } 

} 
window.setTimeout('getSecs1()', 1000) 

現在我想添加一個名爲「測試時間限制」,對實例的用戶只有60分鐘,做測試的新功能:使計數器應開始計數下。

+0

相關:審查倒計時計時器http://codereview.stackexchange.com/questions/10290/countdown-in-javascript –

回答

2

你好,這是修改後的函數,它倒計時功能
的Javascript

document.writeln("<span id=\"time_online1\"></span>"); 
zi_inceput1 = new Date(); 
ceas_start1 = zi_inceput1.getTime(); 

function initStopwatch1() { 
    var timp_pe_pag1 = new Date(); 
    return ((timp_pe_pag1.getTime() + (1000 * 0) - ceas_start1)/1000); 
} 
var tim = 05; 
function getSecs1() { 
    var tSecs1 = Math.round(initStopwatch1()); 
    if((tim-tSecs1)>=0) 
    { 
    var iSecs1 = (tim-tSecs1) % 60; 

    var iMins1 = Math.round((tSecs1 - 30)/60); 
    var iHour1 = Math.round((iMins1 - 30)/60); 
    var iMins1 = iMins1 % 60; 
    var min = Math.floor((tim-tSecs1)/60); 
    var iHour1 = iHour1 % 24; 
    var sSecs1 = "" + ((iSecs1 > 9) ? iSecs1 : "0" + iSecs1); 
    var sMins1 = "" + ((iMins1 > 9) ? iMins1 : "0" + iMins1); 
    var sHour1 = "" + ((iHour1 > 9) ? iHour1 : "0" + iHour1); 
    document.getElementById('time_online1').innerHTML = min + ":" + sSecs1; 
    window.setTimeout('getSecs1()', 1000); 
} 
else 
    alert("time up"); 
} 
window.setTimeout('getSecs1()', 1000)​ 

你可以jsfiddle 測試解決方案。這是否完成您的要求哥們..

更新西北。

+0

嘿男人它不倒計時的分鐘,它只計秒秒 –

+0

檢查代碼nw! !起初我搞砸了更新你的代碼。它工作正常。 – Neji

+0

對不起,我想我的代碼被調整,而不是任何其他倒計時腳本。因爲此代碼的另一部分可以保存cookie中傳遞的時間,並且當用戶刷新頁面時,它將計入當時的時間。我厭倦了我最好的解決方案,但失敗了 –