1

can someone help this code so the timer not resetting again ?

Here is my code :

var mins
var secs;

function cd() {
 	mins = 1 * m("02"); // change minutes here
 	secs = 0 + s(":01"); // change seconds here (always add an additional second to your total)
 	redo();
}

function m(obj) {
 	for(var i = 0; i < obj.length; i++) {
  		if(obj.substring(i, i + 1) == ":")
  		break;
 	}
 	return(obj.substring(0, i));
}

function s(obj) {
 	for(var i = 0; i < obj.length; i++) {
  		if(obj.substring(i, i + 1) == ":")
  		break;
 	}
 	return(obj.substring(i + 1, obj.length));
}

function dis(mins,secs) {
 	var disp;
 	if(mins <= 9) {
  		disp = " 0";
 	} else {
  		disp = " ";
 	}
 	disp += mins + ":";
 	if(secs <= 9) {
  		disp += "0" + secs;
 	} else {
  		disp += secs;
 	}
 	return(disp);
}

function redo() {
 	secs--;
 	if(secs == -1) {
  		secs = 59;
  		mins--;
 	}
 	document.cd.disp.value = dis(mins,secs); // setup additional displays here.
	document.getElementById('waktu_val').innerHTML = dis(mins,secs);
 	if((mins == 0) && (secs == 0)) {
  		//window.alert("Time is up. Press OK to continue.");// change timeout message as required
		window.document.form_verbal.submit(); 
  		//window.location = "http://localhost/tesasisten/Soal/exec_jkl.php" // redirects to specified page once timer ends and ok button is pressed
 	} else {
 		cd = setTimeout("redo()",1000);
 	}
}

function init() {
  cd();
}
window.onload = init;

i'm using it for recruitment test, but since this bug found when refreshing page so the timer go back to 2 minute and needed to be fix. I think code itself not saving the multiple choices the question given.

that's it, any help would be appreciated thanks.

2 Answers 2

1
var mins
    var secs;
var minutes=sessionStorage.mins || "02";
var seconds=sessionStorage.secs || ":01";
    function cd() {
        mins = 1 * m(minutes); // change minutes here
        secs = 0 + s(seconds); // change seconds here (always add an additional second to your total)
        redo();
    }

    function m(obj) {
        for(var i = 0; i < obj.length; i++) {
            if(obj.substring(i, i + 1) == ":")
            break;
        }
        return(obj.substring(0, i));
    }

    function s(obj) {
        for(var i = 0; i < obj.length; i++) {
            if(obj.substring(i, i + 1) == ":")
            break;
        }
        return(obj.substring(i + 1, obj.length));
    }

    function dis(mins,secs) {
        var disp;
        if(mins <= 9) {
            disp = " 0";
        } else {
            disp = " ";
        }
        disp += mins + ":";
        if(secs <= 9) {
            disp += "0" + secs;
        } else {
            disp += secs;
        }
        return(disp);
    }

    function redo() {
        secs--;
        if(secs == -1) {
            secs = 59;
            mins--;
        }
        document.cd.disp.value = dis(mins,secs); // setup additional displays here.
        document.getElementById('waktu_val').innerHTML = dis(mins,secs);
sessionStorage.mins=mins;
sessionStorage.secs=secs;
        if((mins == 0) && (secs == 0)) {
            //window.alert("Time is up. Press OK to continue.");// change timeout message as required
            window.document.form_verbal.submit(); 
            //window.location = "http://localhost/tesasisten/Soal/exec_jkl.php" // redirects to specified page once timer ends and ok button is pressed
        } else {
            cd = setTimeout("redo()",1000);
        }
    }

    function init() {
      cd();
    }
    window.onload = init;

<!-- end snippet -->
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to keep the current timer throught refreshes you can add it to the sessionStorage or the localStorage.

Just make sure you remove the record when the test is finished, specially from the localStorage. The sessionStorage will keep it until the session expires, so there is no problem if you don't delete it yoursef, given that there are no complications later on.

Comments

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.