gpt4 book ai didi

javascript:图像在特定时间发生变化

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

我找到了这个脚本:

<html>
<head>
<script type="text/javascript">
var paths = ['assets/img/head.png','assets/img/tail.png'];
var curPic = 1;
var imgO = new Array();
for(i=0; i < paths.length; i++) {
imgO[i] = new Image();
imgO[i].src = paths[i];
}

function swapImage() {
curPic = (++curPic > paths.length-1)? 0 : curPic;
imgCont.src = imgO[curPic].src;
setTimeout(swapImage,200);
}

window.onload=function() {
imgCont = document.getElementById('img');
swapImage();
}
</script>

</head>
<body>
<div>
<img id="img"/>
</div>
</body>
</html>

这种重复改变图片的时间不受限制,但是我如何改变它重复的情况,例如仅5次?

最佳答案

只需使用跟踪器变量即可。

var to, times = 0; //<-- init the tracker, and a var for the timeout
function swapImage() {
if (times == 5) {
clearTimeout(to); //<-- if already 5 times, cancel timeout and...
return; //<-- ...quit
}
curPic = (++curPic > paths.length-1)? 0 : curPic;
imgCont.src = imgO[curPic].src;
to = setTimeout(swapImage,200); //<-- make the TO accessible via our var
times++;
}

关于javascript:图像在特定时间发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44230627/

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