How can this particular code work on page load itself and even on refresh of page timer value remains ticking only when click submit it stops?
I guess we need to store the timer on localStorage and destroy storage on click submit?
JavaScript
x
84
84
1
$(function() {
2
3
// Some global variables
4
var startTime = 0,
5
elapsed = 0,
6
timerId = 0,
7
$timer = $("#timer");
8
9
function formatTime(time) {
10
var hrs = Math.floor(time / 60 / 60 / 1000),
11
min = Math.floor((time - hrs * 60 * 60 * 1000) / 60 / 1000),
12
sec = Math.floor((time - hrs * 60 * 60 * 1000 - min * 60 * 1000) / 1000);
13
14
hrs = hrs < 10 ? "0" + hrs : hrs;
15
min = min < 10 ? "0" + min : min;
16
sec = sec < 10 ? "0" + sec : sec;
17
18
return hrs + ":" + min + ":" + sec;
19
}
20
21
function elapsedTimeFrom(time) {
22
return formatTime(time - startTime + elapsed);
23
}
24
25
function showElapsed() {
26
$timer.text(elapsedTimeFrom(Date.now()));
27
}
28
29
function startTimer() {
30
// React only if timer is stopped
31
startTime = startTime || Date.now();
32
timerId = timerId || setInterval(showElapsed, 1000);
33
localStorage.setItem('startTime');
34
}
35
36
function pauseTimer() {
37
// React only if timer is running
38
if (timerId) {
39
clearInterval(timerId);
40
elapsed += Date.now() - startTime;
41
startTime = 0;
42
timerId = 0;
43
}
44
}
45
46
function resetTimer() {
47
clearInterval(timerId);
48
$timer.text("00:00:00");
49
startTime = 0;
50
elapsed = 0;
51
timerId = 0;
52
}
53
54
function editTimer() {
55
pauseTimer();
56
$timer.prop("contenteditable", true);
57
$timer.css("border", "1px solid red");
58
}
59
60
function setElapsed() {
61
var time = $timer.text(),
62
arr = time.split(":");
63
$timer.prop("contenteditable", false);
64
$timer.css("border", "1px solid black");
65
elapsed = parseInt(arr[0] * 60 * 60, 10);
66
elapsed += parseInt(arr[1] * 60, 10);
67
elapsed += parseInt(arr[2], 10);
68
elapsed *= 1000;
69
}
70
71
function sendTime() {
72
pauseTimer();
73
// Set hidden input value before send
74
$("[name='time']").val(formatTime(elapsed));
75
}
76
77
$("[name='start']").click(startTimer);
78
$("[name='stop']").click(pauseTimer);
79
$("[name='reset']").click(resetTimer);
80
$timer.dblclick(editTimer);
81
$timer.blur(setElapsed);
82
$("form").submit(sendTime);
83
84
});
JavaScript
1
9
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<div id="timer">00:00:00</div>
3
</h1>
4
<form action="" method="post" ame="xyz">
5
<input type="button" name="start" value="Start" class="btn btn-primary mb-2">
6
<input type="button" name="stop" value="Stop" class="btn btn-primary mb-2">
7
<input type="submit" name="action" value="Submit" class="btn btn-primary mb-2">
8
<input type="hidden" name="time" value="00:00:00">
9
</form>
Advertisement
Answer
You need to set the variable in localStorage not just the name
I have commented out the calls since they do not work in StackSnippets
Just uncomment them when you copy to your server.
JavaScript
1
86
86
1
$(function() {
2
3
// Some global variables
4
var startTime = 0;
5
elapsed = 0,
6
timerId = 0,
7
$timer = $("#timer");
8
9
function formatTime(time) {
10
var hrs = Math.floor(time / 60 / 60 / 1000),
11
min = Math.floor((time - hrs * 60 * 60 * 1000) / 60 / 1000),
12
sec = Math.floor((time - hrs * 60 * 60 * 1000 - min * 60 * 1000) / 1000);
13
14
hrs = hrs < 10 ? "0" + hrs : hrs;
15
min = min < 10 ? "0" + min : min;
16
sec = sec < 10 ? "0" + sec : sec;
17
18
return hrs + ":" + min + ":" + sec;
19
}
20
21
function elapsedTimeFrom(time) {
22
return formatTime(time - startTime + elapsed);
23
}
24
25
function showElapsed() {
26
$timer.text(elapsedTimeFrom(Date.now()));
27
}
28
29
function startTimer() {
30
// React only if timer is stopped
31
// startTime = localStorage.getItem('startTime') || Date.now();
32
startTime || Date.now(); // remove and uncomment above
33
timerId = timerId || setInterval(showElapsed, 1000);
34
// localStorage.setItem('startTime',startTime);
35
}
36
37
function pauseTimer() {
38
// React only if timer is running
39
if (timerId) {
40
clearInterval(timerId);
41
elapsed += Date.now() - startTime;
42
startTime = 0;
43
timerId = 0;
44
}
45
}
46
47
function resetTimer() {
48
clearInterval(timerId);
49
$timer.text("00:00:00");
50
startTime = 0;
51
elapsed = 0;
52
timerId = 0;
53
}
54
55
function editTimer() {
56
pauseTimer();
57
$timer.prop("contenteditable", true);
58
$timer.css("border", "1px solid red");
59
}
60
61
function setElapsed() {
62
var time = $timer.text(),
63
arr = time.split(":");
64
$timer.prop("contenteditable", false);
65
$timer.css("border", "1px solid black");
66
elapsed = parseInt(arr[0] * 60 * 60, 10);
67
elapsed += parseInt(arr[1] * 60, 10);
68
elapsed += parseInt(arr[2], 10);
69
elapsed *= 1000;
70
}
71
72
function sendTime() {
73
pauseTimer();
74
// Set hidden input value before send
75
$("[name='time']").val(formatTime(elapsed));
76
// localStorage.removeItem('startTime');
77
}
78
79
$("[name='start']").click(startTimer);
80
$("[name='stop']").click(pauseTimer);
81
$("[name='reset']").click(resetTimer);
82
$timer.dblclick(editTimer);
83
$timer.blur(setElapsed);
84
$("form").submit(sendTime);
85
86
});
JavaScript
1
9
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<div id="timer">00:00:00</div>
3
</h1>
4
<form action="" method="post" ame="xyz">
5
<input type="button" name="start" value="Start" class="btn btn-primary mb-2">
6
<input type="button" name="stop" value="Stop" class="btn btn-primary mb-2">
7
<input type="submit" name="action" value="Submit" class="btn btn-primary mb-2">
8
<input type="hidden" name="time" value="00:00:00">
9
</form>