gpt4 book ai didi

javascript - 如何在页面加载时延迟此 Javascript 代码?

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

我想在页面加载后延迟此打字效果,而不是立即开始,我该怎么做?

document.addEventListener ('DOMContentLoaded',function(event) {

var dataText = ["This is a typing effect"];

function typeWriter(text, i, fnCallback) {
if (i < (text.length)) {
document.querySelector("h1").innerHTML = text.substring(0, i+1) +'<span aria-hidden="true"></span>';
setTimeout(function() {
typeWriter(text, i + 1, fnCallback)
}, 75);
}
else if (typeof fnCallback == 'function') {
}
}
function StartTextAnimation(i) {
if (typeof dataText[i] == 'undefined'){
setTimeout(function() {
StartTextAnimation(0);
}, 20000);
}
if (i < dataText[i].length) {
typeWriter(dataText[i], 0, function(){
StartTextAnimation(i + 1);
});
}
}
StartTextAnimation(0);
});

最佳答案

因此,您似乎希望在页面加载后延迟一段设定的时间开始动画,而不是延迟直到页面加载。我们都误解了这一点,因为前者的用例比后者少得多。

您可以通过更改这段代码来实现此目的:

    if (i < dataText[i].length) {
typeWriter(dataText[i], 0, function(){
StartTextAnimation(i + 1);
});
}

...进入此:

    if (i < dataText[i].length) {
setTimeout(function() {
typeWriter(dataText[i], 0, function(){
StartTextAnimation(i + 1);
});
}, 5000);
}

...延迟 5 秒(5000 毫秒)

关于javascript - 如何在页面加载时延迟此 Javascript 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46835203/

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