Disable Back Button in browser Not working - javascript

The following code is javascript containing a timer and other 2 functions
`
var sec;
var min;
var counter=setInterval(timer, 1000); //1000 will run it every 1 second
//set the timer to the session variable only for the first time
if(isPostBack == 'false'){
sec=second;
min=minute;
}
//set the timer
function timer()
{
if(isPostBack == 'true')
{
sec = second;
min = minute;
}
sec=sec-1;
if(sec < 0)
{
sec=59;
min--;
}
else
{
min=min;
}
if(sec <=9 )
{
sec = "0"+sec;
}
document.getElementById("lblCountDown").innerHTML= "Time Left: "+(min<=9 ? "0" + min : min)+" mins"+" "+sec + " secs";
//copy the value of min and sec in fields
document.getElementById("min").value=min;
document.getElementById("sec").value=sec;
//copy the value of min and sec in session variable
minute = min;
second = sec;
if (min <=0 && sec <= 0)
{
clearInterval(counter);
alert("Times Up!!!. Your Test Will Be Auto Submited");
document.getElementById("Submit").click();
return;
}
}
function showKeyCode(e)
{
var keycode =(window.event) ? event.keyCode : e.keyCode;
if(keycode == 116)
{
alert("Page Cannot be refresh");
event.keyCode = 0;
event.returnValue = false;
return false;
}
}
function disableBackButton()
{
window.history.forward(1);
}
`
Code in aspx.cs file
`<body onload = "disableBackButton()" onkeydown = "showKeyCode()">
back button is not getting disable
can anyone plz suggest the solution?

That code is not going to disable the back button!
That code would have to be on the previous page.

Related

restricting timer to be reset JavaScript

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

Restart countdwon timer on next button click

i am working on a bid auction website
i have a count down timer script. its working well on load of window, when i click on restart button it should restart the countdown timer from new value but its not working
<script type="text/javascript">
function countDown(hrs,min,sec,gid) {
sec--;
if (sec == -01) {
sec = 59;
min = min - 1;
}
else { min = min; }
if (min == -01) {
min = 59;
hrs = hrs - 1;
}
else { hrs = hrs; }
if (sec<=9) { sec = "0" + sec; }
if (hrs<=9) { hrs = "0" + hrs; }
time = hrs + ":" + (min<=9 ? "0" + min : min) + ":" + sec + "";
if (document.getElementById) { document.getElementById(gid).innerHTML = time; }
SD=window.setTimeout("countDown("+hrs+","+min+","+sec+",'"+gid+"');", 1000);
if (hrs == '00' && min == '00' && sec == '00') { sec = "00"; window.clearTimeout(SD); }
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(function() {
countDown(1,0,10,'oneT');
countDown(0,0,28,'twoT');
countDown(0,2,15,'threeT');
});
</script>
<button onclick="countDown(1,0,10,'oneT')">Restart</button>
<div id="oneT" ></div>
<div id="twoT" ></div>
<div id="threeT"></div>
Thanks, Ahsan
<script type="text/javascript">
function resetCountdown() {
if (SD) {
clearTimeout(SD);
}
countDown(1, 0, 10, 'oneT');
}
</script>
<button onclick="resetCountdown()">Restart</button>
<div id="oneT" ></div>
You need to clear the timeout for every variable that you are using in order to work.
The problem is that your onclick event calls countDown without clearing the queued countDown call from setTimeout that was initiated by your original countDown call. This leads to two competing series of calls to countDown. You can see this using your current code when you click "Restart": sometimes the counter will increase and then decrease, as the two competing strings of countDown calls both change the displayed time.
The solution (with minimal code changes), is to store the setTimeout object in a global variable. You can then use clearTimeout to cancel any pending calls to countDown before restarting the timer. This can be done in a new function, restartTimer.
<html>
<script type="text/javascript">
var timers = {};
function countDown(hrs,min,sec,gid) {
sec--;
if (sec == -01) {
sec = 59;
min = min - 1;
}
else { min = min; }
if (min == -01) {
min = 59;
hrs = hrs - 1;
}
else { hrs = hrs; }
if (sec<=9) { sec = "0" + sec; }
if (hrs<=9) { hrs = "0" + hrs; }
time = hrs + ":" + (min<=9 ? "0" + min : min) + ":" + sec + "";
if (document.getElementById) { document.getElementById(gid).innerHTML = time; }
timers[gid]=window.setTimeout("countDown("+hrs+","+min+","+sec+",'"+gid+"');", 1000);
if (hrs == '00' && min == '00' && sec == '00') { sec = "00"; window.clearTimeout(timers[gid]); }
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(function() {
countDown(1,0,10,'oneT');
countDown(0,0,28,'twoT');
countDown(0,2,15,'threeT');
});
function restartTimer(hrs, min, sec, gid) {
window.clearTimeout(timers[gid]);
timers[gid] = countDown(hrs, min, sec, gid);
}
</script>
<button onclick="restartTimer(1,0,10,'oneT')">Restart</button>
<div id="oneT" ></div>
<div id="twoT" ></div>
<div id="threeT"></div>
</html>

clearInterval() not working

Session length is the start time of the timer, default is 25. isEven is used to start/stop the timer, if isEven is false, the timer should start, if it is odd it should hit clearInterval, which for some reason is not stopping execution of tick() function (which counts down the timer).
var count = 0;
function countdown(sessionLength) {
var minutes = sessionLength - 1;
var seconds = 60;
var isEven = false;
count++;
if (count % 2 == 0) {
isEven = true;
} else {
isEven = false;
}
var myVar = setInterval(tick, 1000);
if (isEven == false) {
function tick() {
if (seconds > 0) {
seconds--;
} else {
minutes--;
seconds = 59;
}
if (minutes > 0) {
document.getElementById("time").innerHTML =
minutes.toString() + ":" + (seconds < 10 ? "0" : "") + seconds.toString();
} else {
document.getElementById("time").innerHTML =
"0:" + (seconds < 10 ? "0" : "") + seconds.toString();
}
}
} else {
console.log("reached");
clearInterval(myVar);
}
};
Move the tick() function declaration outside of the if block
setInterval variable is a local one. So its value is overwritten every time you call countdown
. The solution would be to initialize myVar outside the function countdown or to make it a global variable like this :
window.myVar =setInterval (tick,1000);
You have also to put this instruction in the first if block, so that it is not overwritten everytime
var count = 0;
function countdown(sessionLength) {
var minutes = sessionLength - 1;
var seconds = 60;
var isEven = false;
count++;
if (count % 2 == 0) {
isEven = true;
} else {
isEven = false;
}
if (isEven == false) {
window.myVar = setInterval(tick, 1000);
function tick() {
if (seconds > 0) {
seconds--;
} else {
minutes--; seconds = 59;
}
if (minutes > 0) {
document.getElementById("time").innerHTML = minutes.toString() + ":" + (seconds < 10 ? "0" : "") + seconds.toString();
} else {
document.getElementById("time").innerHTML = "0:" + (seconds < 10 ? "0" : "") + seconds.toString();
}
}
} else {
console.log("reached");
if (window.myVar){
clearInterval(window.myVar);
window.myVar = null;
}
}
};

Button Click shouldn't stack the function it triggers but it does

I'm making an timer in Javascript which already works well the only problem is when I keep hitting the
"Start" button it keeps stacking the count function and will count at an higher and higher speed. Tried multiple things to stop it but can't manage myself.
JS:
var startknop = document.getElementById("start");
var stopknop = document.getElementById("stop");
var hour = 0;
var min = 0;
var sec = 0;
startknop.onclick = function()
{
intervalId = setInterval(count, 1000);
}
stopknop.onclick = function()
{
clearInterval(intervalId);
}
counter.innerHTML = hour + "0:" + min + "0:" + sec + "0";
function count() {
sec++;
if(sec <= 9)
{
var seco = "0" + sec;
}
else
{
seco = sec;
}
if(min <= 9)
{
var mino = "0" + min;
}
else
{
mino = sec;
}
if(hour <= 9)
{
var houro = "0" + hour;
}
else
{
houro = hour;
}
if(sec == 60){
sec = 0;
min += 1;
}
if(min == 60){
min = 0;
hour += 1;
}
counter.innerHTML = houro + ":" + mino + ":" + seco;
}
var running = false;
startknop.onclick = function()
{
if(!running)
{
running=true;
intervalId = setInterval(count, 1000);
}
}
when timer is finished set running false.
Wrap the setInterval in an if block with a boolean to check if initialised. You are starting multiple timers, so stop only stops the last one assigned to your global variable. You could also set intervalId to -1 and do a check on it being >0, setting it back to -1 after the clearInterval call.

Fix timer starting on page load

A simple Javascript timer that runs for 30 seconds and displays a message once it reaches 0. I cant figure out how to get it to start when a button is pressed instead of on pageload. Any help is appreciated :)
<script type="text/javascript">
var sec = 30; // set the seconds
var min = 00; // set the minutes
function countDown() {
sec--;
if (sec == -01) {
sec = 59;
min = min - 1;
} else {
min = min;
}
if (sec<=9) { sec = "0" + sec; }
time = (min<=9 ? "0" + min : min) + " min and " + sec + " sec ";
if (document.getElementById) { theTime.innerHTML = time; }
SD=window.setTimeout("countDown();", 1000);
if (min == '00' && sec == '00') { sec = "00"; window.clearTimeout(SD); alert("Too slow."); }
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(function() {
countDown();
});
</script>
<button onclick='countDown();'>Click Me</button>
And just don't call addLoadEvent() at all.
Add event listener to the button in the load function and then call countDown
addLoadEvent(function() {
var el = document.getElementById("startButton");
el.addEventListener("click", countDown, false);
});
Here the full example link
Here's a full sample with start/stop and remaining time display.
var gTimer = null
function endOfTimer() {
console.log("here");
window.alert('Too slow');
}
function start() {
gTimer = window.setTimeout('endOfTimer', 1 * 1000);
}

Categories

Resources