我有ROR应用程序,并向其中添加了倒数计时器。 计时器从15计数到零,然后消失。我正在做的是防止再次刷新页面上计数,因此,我将起始值存储在localStorage中。 但是我需要在用户注销时重置该值,并在再次登录时再次开始计数,这是我的代码:
function startTimer(duration, display) {
var start = localStorage.getItem("start"),
diff,
minutes,
seconds;
var shown = false;
if (start === null) {
start = Date.now();
localStorage.setItem("start", start);
}
function timer() {
diff = duration - (((Date.now() - start) / 1000) | 0);
minutes = (diff / 60) | 0;
seconds = (diff % 60) | 0;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = minutes + ":" + seconds;
if (diff <= 0) {
start = 0;
$('#testDiv').hide();
if (shown == false){
$('#congModal').modal('show');
shown = true;
}
}
}
timer();
setInterval(timer, 1000);
}
window.onload = function () {
var fifteenMinutes = 60 * 15,
display = document.querySelector('#time');
startTimer(fifteenMinutes, display);
};
我认为:
`%li.list-inline-item.g-mx-10--xl.g-font-size-16
#testDiv{"data-value" => "show"}
%span= "#{t('layouts.navbar.countdown.next_reward_in')}"
%span#time 15:00
= "#{t('layouts.navbar.countdown.minutes')}"
%i{"data-target" => "#congModal", "data-toggle" => "modal"} `
答案 0 :(得分:0)
这解决了问题!
在js文件上:
`function cleanUp(){
localStorage.removeItem('start')
}`
查看:
%a{ "data-method" => "delete", href: destroy_user_session_path, onclick: "cleanUp();"}