gpt4 book ai didi

javascript - 如何使在 onolad 上工作的 JS 函数在第一页加载时不起作用?

转载 作者:行者123 更新时间:2023-11-28 19:14:44 25 4
gpt4 key购买 nike

我有一个 html 页面,其中包含一个在页面加载后即可运行的函数。

<body onload="startNow()">

这个startNow函数有一个部分,如果不满足某个条件,就会刷新页面,从而导致该函数再次运行。

function startNow() {
var warning_check = document.getElementsByClassName("warning");
if (warning_check.length == 0) {
checkAll(); //another function call
} else {
window.location.reload();
}
}

有没有办法让这个 startNow 函数在第一次加载此页面时不运行?

最佳答案

使用本地存储轻松完成

function startNow(){
var warning_check = document.getElementsByClassName("warning");
if(warning_check.length == 0) {
checkAll(); //another function call
} else {
if (!!localStorage.returningUser){
window.location.reload();
}
}
localStorage.returningUser = 'true';
}

编辑

上述函数对于首次使用的用户仍然运行,但如果代码到达重新加载点,它不会重新加载。如果您想让代码完全停止运行,那么

function startNow(){
if (!localStorage.returningUser){
localStorage.returningUser = 'true';
return false;
}
var warning_check = document.getElementsByClassName("warning");
if(warning_check.length == 0) {
checkAll(); //another function call
} else {
window.location.reload();
}
}

关于javascript - 如何使在 onolad 上工作的 JS 函数在第一页加载时不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30112813/

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