如何创建一个Javascript横幅在不同时间切换图片?
我有一个横幅,里面有 10 张图片。我知道如何设置超时,以便图片每隔一定的秒数切换一次,但是我如何设置一个计时器来根据我想要单个图片显示的时间来更改图片。
例如: 我希望图片1显示10秒,图片2显示3秒,图片3显示15秒。
到目前为止,这是我的代码:(以 5 秒的相等间隔更改所有图像。Javascript
:
window.onload = rotate;
var thisAd = 0;
var adImages = new Array("Images1/Picture10","Images1/Picture1","Images1 /Picture2","Images1/Picture3","Images1/Picture4","Images1/Picture5","Images1/Picture6","Images1/Picture7","Images1/Picture8","Images1/Picture9");
function rotate(){
thisAd++;
if(thisAd == adImages.lengh){
thisAd = 0;
}
document.getElementById("adBanner").src = adImages[thisAd];
setTimeout(rotate, 5 * 1000);
}
I have a banner that has 10 pictures in it. I know how to set a timeout so the pictures switches every certain amount of seconds but how could I set a timer to change the picture based on how long I want individule ones displayed for.
For example:
I want picture1 displayed for 10 seconds, picture2 displayed for 3 seconds and picture3 displayed for 15 seconds.
This is my code so far: (Which changes the all images at equal intervals of 5 seconds.
Javascript:
window.onload = rotate;
var thisAd = 0;
var adImages = new Array("Images1/Picture10","Images1/Picture1","Images1 /Picture2","Images1/Picture3","Images1/Picture4","Images1/Picture5","Images1/Picture6","Images1/Picture7","Images1/Picture8","Images1/Picture9");
function rotate(){
thisAd++;
if(thisAd == adImages.lengh){
thisAd = 0;
}
document.getElementById("adBanner").src = adImages[thisAd];
setTimeout(rotate, 5 * 1000);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建一个时间数组以匹配您的图像。:
使用计时器中的值
Make a times array to match your images.:
Use the value in the timer
您可以将图片存储到具有两个属性的对象中:一个用于 URL,另一个用于延迟,然后使用它。
然后您可以使用这些属性:
You could store your pictures into objects with two properties: one for the URL, the other for the delay, then use that.
Then you can use those properties:
由于您的计时没有逻辑,因此无法创建通用算法。因此,您必须为每个广告执行 switch / else if。
您还必须在一个周期后重置计数器
thisAd
。As your timeing has no logic a general algorithm can not be created. So you have to do a switch / else if for each ad.
You will also have to reset the counter,
thisAd
, after one cycle.