I have ROR application and i added countdown timer to it . The timer count from 15 to zero then disappear. What i'm doing is to prevent count again on refresh page , so , i stored start value in localStorage. But i need to reset the value when user log out and start count again when login again, here is my code:
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);
};
and on view i have:
`%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"} `
This solved the problem!
on js file:
`function cleanUp(){
localStorage.removeItem('start')
}`
on view:
%a{ "data-method" => "delete", href: destroy_user_session_path, onclick: "cleanUp();"}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.