简体   繁体   English

"每次重新加载页面时,Javascript Timer Counter 都会重新启动"

[英]Javascript Timer Counter start again for every page reload

I build a timer to count Daily working time of employee.我建立了一个计时器来计算员工的每日工作时间。 It is completely working fine until i refresh the page.在我刷新页面之前,它完全可以正常工作。 I have three button clock in, stop, clock out.我有三个按钮时钟,停止,时钟。 It should counting the time until I press the clock out button.它应该计算时间,直到我按下打卡按钮。 What should I do in this case?在这种情况下我该怎么办? I am not good with javascript so if anyone would love to help me, it would be great.我不擅长 javascript,所以如果有人愿意帮助我,那就太好了。

                       <form method="post" id="work-hour" action=""> 
                                {% csrf_token %}
                                <div class="card" id="holidays">
                                    <div class="card-header">
                                        <h5>Work Timer and Weekly Work History</h5>
                                        <div id="timeDiv"></div> 
                                        <br>
                                        <button id="startBtn" class="btn btn-outline-success Btn">Clock In</button>
                                        <!-- <button id="" class="Btn btn btn-outline-primary">Take a break</button> -->
                                        <button id="stopBtn" class="btn btn-outline-warning Btn">Stop</button>   
                                        <input type="text" hidden id="total_time_hour"  />
                                        <input type="text" hidden id="total_time_minute" />
                                        <input value="Clock Out" type="submit" id="submit" />                                    </div>
                                    
                                     </div>
                                
                       </form>

Maybe you can storage last counter result in localstorage.也许您可以将最后一个计数器结果存储在本地存储中。

localStorage.setItem('lastCounter', total_time); localStorage.setItem('lastCounter', total_time);

const lastCounter = localStorage.getItem('lastCounter'); const lastCounter = localStorage.getItem('lastCounter');

Using localStorage allows save the data even you lost the browser session or reload the page.即使您丢失了浏览器会话或重新加载页面,使用 localStorage 也可以保存数据。

You can do the same effect with cookies, but they have expiration.您可以使用 cookie 实现相同的效果,但它们有过期时间。

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM