0

please help, theres 2 things i want to asking about:

  1. with that script, the count down just show an number, iwant the countdown shown as time format --:--:--
  2. i cant auto submit after the countdown finish, and the count down wont stop after zero.

    <script>
    function timeOut(){
    alert("timeout");
    document.getElementById('myFormId').submit();
    }       
    (function () {
    var timeLeft = <?php echo ($data_test['TIME'] * 60) - $tb ?>,
        cinterval;
    
    var timeDec = function (){
        timeLeft--;
        document.getElementById('timer').innerHTML = timeLeft;
        if(timeLeft === 0){
            timeOut();
            clearInterval(cinterval);
        }
    };
    
    cinterval = setInterval(timeDec, 1000);
    })();
    </script>
    

1 Answers1

0
var interval = 11; // Whatever.
function myTimer(interval){
  document.getElementById('timer').innerHTML = --interval;
  if (interval > 0) setTimeout(function(){myTimer(interval);}, 1000);
  else document.forms['myFormId'].submit();
}
myTimer(interval);

Something along these lines. Could be a lot better, but it's really just to give you an example right here. Shouldn't be much of a hazle.

Edit: Also see JavaScript seconds to time string with format hh:mm:ss for the formatting.

Community
  • 1
  • 1
Fabian Schneider
  • 345
  • 2
  • 10