gpt4 book ai didi

javascript - 超时工作

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

我在页面上有一个链接,并且希望在 onLoad 后定义的超时后,此链接将被另一个链接替换。我有以下示例代码:

window.addEventListener('DOMContentLoaded', changeLinks, false);

function timeout_init() {
setTimeout('changeLinks()', 30000);
}

window.onload = changeLinks;

var _linksChanged = false;
function changeLinks() {
if(_linksChanged) return;
_linksChanged = true;
var aEls = document.getElementsByTagName('a');
for (var i = 0, aEl; aEl = aEls[i]; i++) {
aEl.href = aEl.href.replace('https://google.com','https://yahoo.com');
}
}
<a href="https://google.com" target="_blank">link</a>

问题:超时不起作用,链接立即被替换

只有在绑定(bind)到onLoad事件超时后才替换链接,应该怎么做?

最佳答案

window.onload = function() {
var _linksChanged = false;
setTimeout(changeLinks, 30000);
}
function changeLinks() {
if(_linksChanged) return;
_linksChanged = true;
var aEls = document.getElementsByTagName('a');
for (var i = 0, aEl; aEl = aEls[i]; i++) {
aEl.href =
aEl.href.replace('https://google.com','https://yahoo.com');
}
}

关于javascript - 超时工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51421038/

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