gpt4 book ai didi

javascript - 为什么我的脚本不会在每次页面加载时运行?

转载 作者:行者123 更新时间:2023-11-30 12:37:49 25 4
gpt4 key购买 nike

这是我当前的代码,它是一个在我的网站加载时自动输入一些文本的循环。问题是它非常不稳定,它只在某些时候起作用(通常是第一次加载,而不是在刷新时等)。有人可以指出这个问题吗?

var i = 0;   
var line_1 = " Understand their core goal.. Act upon the emotion..";
var line_2 = " Then..";
var line_3 = " Create your own luck!";
var all = line_1 + "{" + line_2 + "{" + line_3 + "{{";
var has = "";
var time = 100;
var hit = 0;
function myLoop () {
setTimeout(function () {
if(all.charAt(i) == "{") {
//has +1"<br>";
time = 2000;
hit++;
if(hit == 3){
document.getElementsByName('cbar')[0].placeholder = 'Enter your email address to learn more';
}

}else{
has += (all.charAt(i));
time = 100;
}
if(hit == 4){
document.getElementById('cbar').value = "";
}else{
document.getElementById('cbar').value = has;
}

if(all.charAt(i) == "{" || hit == 3){
has = "";
}
i++;
if (i < all.length) {
myLoop();
}
}, time)
}
myLoop();

最佳答案

试着把你的代码放在 window.onload = function() {//你的代码在这里...}; 它应该足以包装以下内容:

window.onload = function() {
var i = 0;
var line_1 = " Understand their core goal.. Act upon the emotion..";
var line_2 = " Then..";
var line_3 = " Create your own luck!";
var all = line_1 + "{" + line_2 + "{" + line_3 + "{{";
var has = "";
var time = 100;
var hit = 0;
myLoop();
}

myLoop 函数定义应该在 block 之外。

关于javascript - 为什么我的脚本不会在每次页面加载时运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25524351/

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