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.
JavaScript
x
32
32
1
<div id='timer'></div>
2
<div id='progress' style='background:red; height:5px;'></div>
3
<script>
4
function started(duration) {
5
var TotalSeconds = "<?php echo $secs; ?>";
6
var documentWidth = $(document).width();
7
var start = Date.now();
8
var intervalSetted = null;
9
10
function timer() {
11
var diff = duration - (((Date.now() - start) / 1000) | 0);
12
var seconds = (diff % 60) | 0;
13
seconds = seconds < 10 ? "0" + seconds : seconds;
14
$('#timer').html("00:" + seconds);
15
var progresBarWidth = (seconds * documentWidth / TotalSeconds);
16
17
$('#progress').css({
18
width: progresBarWidth + 'px'
19
});
20
21
if (diff <= 0) {
22
clearInterval(intervalSetted);
23
}
24
}
25
26
timer();
27
intervalSetted = setInterval(timer, 1000);
28
}
29
30
started("<?php echo $secs; ?>");
31
</script>
32
Advertisement
Answer
You need to convert
duration to time format.
JavaScript
1
37
37
1
<div id='timer'></div>
2
<div id='progress' style='background:red; height:5px;'></div>
3
<script>
4
function started(duration) {
5
var TotalSeconds = duration;
6
var documentWidth = $(document).width();
7
var start = Date.now();
8
var intervalSetted = null;
9
10
function timer() {
11
var diff = duration - (((Date.now() - start) / 1000) | 0);
12
var seconds = (diff % duration) | 0;
13
seconds = seconds < 10 ? "0" + seconds : seconds;
14
15
var date = new Date(0);
16
date.setSeconds(seconds);
17
var timeString = date.toISOString().substr(11, 8);
18
19
$('#timer').html(timeString);
20
var progresBarWidth = (seconds * documentWidth / TotalSeconds);
21
22
$('#progress').css({
23
width: progresBarWidth + 'px'
24
});
25
26
if (diff <= 0) {
27
clearInterval(intervalSetted);
28
}
29
}
30
31
timer();
32
intervalSetted = setInterval(timer, 1000);
33
}
34
35
started("<?php echo $secs; ?>");
36
</script>
37
JavaScript
1
32
32
1
function started(duration) {
2
var TotalSeconds = duration;
3
var documentWidth = $(document).width();
4
var start = Date.now();
5
var intervalSetted = null;
6
7
function timer() {
8
var diff = duration - (((Date.now() - start) / 1000) | 0);
9
var seconds = (diff % duration) | 0;
10
seconds = seconds < 10 ? "0" + seconds : seconds;
11
12
var date = new Date(0);
13
date.setSeconds(seconds);
14
var timeString = date.toISOString().substr(11, 8);
15
16
$('#timer').html(timeString);
17
var progresBarWidth = (seconds * documentWidth / TotalSeconds);
18
19
$('#progress').css({
20
width: progresBarWidth + 'px'
21
});
22
23
if (diff <= 0) {
24
clearInterval(intervalSetted);
25
}
26
}
27
28
timer();
29
intervalSetted = setInterval(timer, 1000);
30
}
31
32
started(60);
JavaScript
1
3
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<div id='timer'></div>
3
<div id='progress' style='background:red; height:5px;'></div>