gpt4 book ai didi

javascript - 如何使用rails注销用户后清除localStorage?

转载 作者:行者123 更新时间:2023-12-03 01:16:04 25 4
gpt4 key购买 nike

我有 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);

};

在 View 中我有:

`%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"} `

最佳答案

这解决了问题!

在 js 文件上:

`function cleanUp(){
localStorage.removeItem('start')
}`

查看:

%a{ "data-method" => "delete", href: destroy_user_session_path, onclick: "cleanUp();"}

关于javascript - 如何使用rails注销用户后清除localStorage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52008439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com