-2

Hi guys i am using a timer for 20 mins in my code.(Its working)

But when ever i press back button or reload button its restarting. how to avoid this ? any one got code to disable back button in browser ?

Please any one can help me with this.

<html>
<head>
<script language="javascript">

function timedText()
{

var x=document.getElementById('txt');
var wc=setTimeout(function(){document.getElementById("txt").disabled=true;},100);
var tu=setTimeout(function(){x.value="20 min "},100);

var flag=1200000;
var to = setInterval(function(){if(flag > 0)
                                {
                                    x.value= flag/60000 + " min";
                                    flag-=60000;
                                }
                                else
                                {
                                    document.getElementById("submit_id").click();
                                    clearInterval(to);
                                }
                               },60000);
}
</script>
</head>
<body onload="timedText()">

<form action="">

<p align='right'><b>Time left </b><input type="text" id="txt" /></p>

</form>
</body>
</html>
RedSun
  • 107
  • 2
  • 2
  • 10
  • What's the specific issue? How to count seconds instead of minutes? That's some relatively simple math changes. How to display each on its own? Actually the same thing; math. – Dave Newton Oct 01 '13 at 20:02
  • http://stackoverflow.com/questions/16156832/javascript-countdown-to-next-real-5-minutes – twodayslate Oct 01 '13 at 20:02
  • I'll give you a hint: `x.value= flag/60000 + " min";` – Jim Oct 01 '13 at 20:12
  • 1
    When a page reloads, all JavaScript on the page will be re-evaluated. That can't be avoided. I think the real question is, why do you need a timer that continues to run after the user leaves your page? If we knew the answer to that we might be able to suggest an appropriate solution. Disabling the back button is a big no-no from a user experience stand point. Users universally hate having their back buttons disabled. – rescuecreative Oct 02 '13 at 19:53

1 Answers1

1

If i onderstand what you want is a countdown with seconds:

// set the date we're counting down to
var target_date = new Date("Aug 15, 2019").getTime();

// variables for time units
var days, hours, minutes, seconds;

// get tag element
var countdown = document.getElementById("countdown");

// update the tag with id "countdown" every 1 second
setInterval(function () {

    // find the amount of "seconds" between now and target
    var current_date = new Date().getTime();
    var seconds_left = (target_date - current_date) / 1000;

    // do some time calculations
    days = parseInt(seconds_left / 86400);
    seconds_left = seconds_left % 86400;

    hours = parseInt(seconds_left / 3600);
    seconds_left = seconds_left % 3600;

    minutes = parseInt(seconds_left / 60);
    seconds = parseInt(seconds_left % 60);

    // format countdown string + set tag value
    countdown.innerHTML = days + "d, " + hours + "h, "
    + minutes + "m, " + seconds + "s";  

}, 1000);

And in html:

<span id="countdown"></span>

I hope this helped.

Onovar
  • 729
  • 1
  • 7
  • 19
  • I have to perform this task after timeout else { document.getElementById("submit_id").click(); } – RedSun Oct 01 '13 at 20:54