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>
Advertisement
Answer
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>