2

we have an online quiz section on our website , and it has a javascript code that count up the time our testers are participating a particular test :

fiddle

526 in this code means the seconds passed from the quiz start

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)

now I want to add a new feature called "test time limit" that for instance the user has only 60 minutes to do the test : so the counter should start counting down.

1

1 Answer 1

2

hi this is your modified function that does count down functionality
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)​

you can test the solution on jsfiddle . does that complete your requirement buddy..

Updated nw.

Sign up to request clarification or add additional context in comments.

3 Comments

hey man it doesn't count down the minutes ,it only counts the seconds
Check the code nw!! at first i messed up with updating your code. nw it works fine.
sorry i want my code to be adjusted not any other countdown script. because there is another part of this code that saves the passed time in cookie and when the user refresh the page , it counts + that time. I tired my best to fix that minute thing , but failed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.