我有脚本咆哮,我需要创建一个函数,更改我的图像,然后用它与setInterval每2秒更改一次图像。如何创建一个函数并在函数上调用它
计数器(控制)工作,但它不会更改图像。
我到底做错了什么?
var contar = 0; //counter value
var pausa = false;
function passar(){ //function that changes the image
$("#"+contar).click(function(){
$(".img").attr('src', 'imagens/'+contar+'.jpg'); //the image is from 1.jpg to 4.jpg
$("#"+contar).addClass('active');
console.log(contar);
});
}
setInterval(function(){ //setinterval to use the function "passar" to change image
contar++;
if(contar<=4) { //my limit of images on the html
passar(contar);
console.log(contar);
}else{
contar = 0;
};
}, 2000);
+2
请包括您的HTML。我们不知道'.img'可能(或可能不会引用)。此外,[JavaScript控制台]中的任何错误(http://webmasters.stackexchange.com/questions/8525/how-to-open-the-javascript-console-in-different-browsers)? –
+0
passar()应该带一个参数contar - '函数passar(contar){'编辑:nm,只是看到你有全局变量 –
+0
@dave或者不要传入;因为它是被忽略的,并且在任何地方都会使用外部的'contar'。不是OP说计数器正在工作,这是图像变化没有发生。 –