1

I am new to javascript/jquery. I found the following example on the internet and I am trying to get it working with my SQL variable. But I am stuck because all it does is count down from 60 over and over again..

What I am trying to accomplish is the following. I have a variable which says how many seconds a user needs to wait before it can perform the action again $secs. What I need is to have the time and process-bar countdown with the seconds from the variable to zero. After that I will add a page reload line to it. But first the timer needs to work. I would really appreciate any help as I can not find any workable solution/explanation for my problem.

    <div id='timer'></div>
    <div id='progress' style='background:red; height:5px;'></div>
    <script>
    function started(duration) {
        var TotalSeconds = "<?php echo $secs; ?>";
        var documentWidth = $(document).width();
        var start = Date.now(); 
        var intervalSetted = null;
    
        function timer() {
            var diff = duration - (((Date.now() - start) / 1000) | 0);
            var seconds = (diff % 60) | 0;
            seconds = seconds < 10 ? "0" + seconds : seconds;
            $('#timer').html("00:" + seconds);
            var progresBarWidth = (seconds * documentWidth / TotalSeconds);
    
            $('#progress').css({
                width: progresBarWidth + 'px'
            });
    
            if (diff <= 0) {
                clearInterval(intervalSetted);
            }
        }
    
        timer();
        intervalSetted = setInterval(timer, 1000);
    }
    
    started("<?php echo $secs; ?>");
    </script>

1 Answer 1

1

You need to convert duration to time format.

<div id='timer'></div>
<div id='progress' style='background:red; height:5px;'></div>
<script>
function started(duration) {
    var TotalSeconds = duration;
    var documentWidth = $(document).width();
    var start = Date.now(); 
    var intervalSetted = null;

    function timer() {
        var diff = duration - (((Date.now() - start) / 1000) | 0);
        var seconds = (diff % duration) | 0;
        seconds = seconds < 10 ? "0" + seconds : seconds;

        var date = new Date(0);
        date.setSeconds(seconds);
        var timeString = date.toISOString().substr(11, 8);

        $('#timer').html(timeString);
        var progresBarWidth = (seconds * documentWidth / TotalSeconds);

        $('#progress').css({
            width: progresBarWidth + 'px'
        });

        if (diff <= 0) {
            clearInterval(intervalSetted);
        }
    }

    timer();
    intervalSetted = setInterval(timer, 1000);
}

started("<?php echo $secs; ?>");
</script>

function started(duration) {
    var TotalSeconds = duration;
    var documentWidth = $(document).width();
    var start = Date.now(); 
    var intervalSetted = null;

    function timer() {
        var diff = duration - (((Date.now() - start) / 1000) | 0);
        var seconds = (diff % duration) | 0;
        seconds = seconds < 10 ? "0" + seconds : seconds;
        
        var date = new Date(0);
        date.setSeconds(seconds);
        var timeString = date.toISOString().substr(11, 8);
          
        $('#timer').html(timeString);
        var progresBarWidth = (seconds * documentWidth / TotalSeconds);

        $('#progress').css({
            width: progresBarWidth + 'px'
        });

        if (diff <= 0) {
            clearInterval(intervalSetted);
        }
    }

    timer();
    intervalSetted = setInterval(timer, 1000);
}

started(60);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='timer'></div>
<div id='progress' style='background:red; height:5px;'></div>

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

9 Comments

Thank for you input. But I do not see my SQL/php $secs variable that has the secconds comming back in you answers. Also the progress bar is not degressing when the timer counts down?
I treid the example with the started and in there the echo for my secconds. It gives me the following error. Uncaught RangeError: Invalid time value at Date.toISOString (<anonymous>) at timer (index.php:126)
I tried the last example. Changed started(60); to started('<?php echo $secs; ?>'); and this seems to do the trick ! Thanks for you help !
I accepted your answer. Thanks for your help !
Maybe you know this to ( I hope so) How do I make the page reload afhter the timer reaches zero? I tried adding it right after the if (diff <= 0) { clearInterval(intervalSetted); location.reload(); But this causes the page to reload all of the time when the timer is timed out.
|

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.