每秒更改属性 [英] Change Attribute Every Second
本文介绍了每秒更改属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望图像每秒更改一次.我在使用setInterval时遇到问题.有人可以快速发布有关此操作的摘要
I want an image to change every second. I'm having trouble with setInterval. Could someone post a quick snippet on how to do this
这就是我想出的.
var images = 'images/image_*.png';
for(var i = 1; i <= 5; i++){
function changeImg(){
var path = images.replace('*', i);
$('img').attr('src', path);
}
setInterval('changeImg()', 1000);
}
推荐答案
在您的代码中,您将调用setInterval函数5次,这实际上是不必要的.同样,由于循环将执行一次,因此i
的值将始终为5,因此它将无法按预期工作.您可以尝试以下方法:
In your code you are calling the setInterval function 5 times which really is not necessary. Also as the loop will execute once, the value of i
will always be 5 so it won't work as you expect. You may try this instead:
var images = 'images/image_*.png';
var i = 1;
setInterval(function() {
var path = images.replace('*', i);
$('img').attr('src', path);
i = i + 1;
if (i == 6) i = 1;
}, 1000);
这篇关于每秒更改属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文