gpt4 book ai didi

javascript - 在多次回发中保留 JQuery 变量值

转载 作者:行者123 更新时间:2023-12-02 15:56:19 24 4
gpt4 key购买 nike

我想要一个 bool 变量来保留其值,即使页面在 Jquery 中重新加载也是如此。这就是我正在做的事情。

    var TimedOut = false;
alert("Time Out Value = " + TimedOut);
if (!TimedOut) {
//do some Processing and stay on same Page
Timedout = true;
// redirect to current Page using window.location.href on session timeout

}

我在循环中设置的 Timedout 值始终为 false,而 if 语句始终为 true。我希望它只加载一次 false,一旦设置为 true,它应该保留该值。我用它来决定是否需要重定向到主页。

不确定是否可以通过 JQuery 执行此操作。

最佳答案

尝试使用这个库可以做到这一点的cookie,https://github.com/js-cookie/js-cookie

Cookies.set('timeout', 'false');
var timeout = Cookies.get('timeout');
alert("Time Out Value = " + TimedOut);
if (!TimedOut) {
//do some Processing and stay on same Page
Cookies.set('timeout', 'true');
// redirect to current Page using window.location.href on session timeout

}

我只是替换了您的代码并添加了 cookie 功能,但您更了解其中的逻辑。

希望对您有所帮助。

关于javascript - 在多次回发中保留 JQuery 变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31527135/

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