[英]Why won't my script run every time the page loads?
這是我當前的代碼,它是一個循環,在我的網站加載時自動輸出一些文本。 問題是它是非常觸摸和去,它只有時有效(通常是第一次加載,而不是刷新等)有人可以指出問題嗎?
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() {//Your code here...};
它應該足以包裝以下內容:
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
函數定義應該在塊之外。
嘗試這個:
HTML:
<body onload="myFunction()">
使用Javascript:
function myFunction(){
//your code here
}
嘗試這個:
$( document ).ready(function() {
//你的代碼在這里});
PS:這是JQuery。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.