Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript递归地更改图像_Javascript - Fatal编程技术网

javascript递归地更改图像

javascript递归地更改图像,javascript,Javascript,我正在尝试使用javascript递归函数在我的站点上延迟递归地更改图像。以下是我的js代码: const ns=0; 旋转木马(ns); 功能转盘(n){ 如果(n3){ n=0; } var x=document.getElementsByClassName(“ovalImg”); x[0].src=“Images/cooperation/Oval_1.1.png”; x[1].src=“Images/cooperation/Oval_2.1.png”; x[2].src=“Images/

我正在尝试使用javascript递归函数在我的站点上延迟递归地更改图像。以下是我的js代码:

const ns=0;
旋转木马(ns);
功能转盘(n){
如果(n<0 | | n>3){
n=0;
}
var x=document.getElementsByClassName(“ovalImg”);
x[0].src=“Images/cooperation/Oval_1.1.png”;
x[1].src=“Images/cooperation/Oval_2.1.png”;
x[2].src=“Images/cooperation/Oval_3.1.png”;
n++;
如果(n>x.length){n=1}
x[n-1].src=“Images/cooperation/Oval”+n+.png”;
setTimeout(carousel(n),5000);//每2秒更改一次图像
}

Pecode软件| IT公司

请试试这个。。。您必须从转盘func中删除超时func,并将其粘贴到代码开头。。。就像下面

const ns = 45;
setTimeout(carousel(ns), 5000);


function carousel(n) {
    if(n < 0 || n > 3){
        n = 0;
    }
    var x = document.getElementsByClassName("cooperation-ovalImg");
    x[0].src = "Images/cooperation/Oval_1.1.png";
    x[1].src = "Images/cooperation/Oval_2.1.png";
    x[2].src = "Images/cooperation/Oval_3.1.png";
    n++;
    if (n > x.length) {n = 1}
    x[n-1].src = "Images/cooperation/Oval_"+n+".png";
     // Change image every 2 seconds
}
常数ns=45;
设置超时(旋转木马(ns),5000);
功能转盘(n){
如果(n<0 | | n>3){
n=0;
}
var x=document.getElementsByClassName(“ovalImg”);
x[0].src=“Images/cooperation/Oval_1.1.png”;
x[1].src=“Images/cooperation/Oval_2.1.png”;
x[2].src=“Images/cooperation/Oval_3.1.png”;
n++;
如果(n>x.length){n=1}
x[n-1].src=“Images/cooperation/Oval”+n+.png”;
//每2秒更改一次图像
}
您应该使用超时,而不是超时。您的环境告诉您,无法退出该功能。这是个坏习惯

您应该使用类似以下内容:

var myinterval;

myinterval = setInterval(function(){
    // myFunction(params)
}, 5000);

function myFunction(params){
    // Your stuff in here
}

// To exit or finish your stuff you will call
/* clearInterval(myinterval); */
$(文档).ready(函数(){
var元素=$(“.limg”);
var照片=[
'https://www.countryflags.io/be/flat/64.png',
'https://www.countryflags.io/lk/flat/64.png',
'https://www.countryflags.io/us/flat/64.png',
'https://www.countryflags.io/in/flat/64.png',
'https://www.countryflags.io/jp/flat/64.png',
'https://www.countryflags.io/aq/flat/64.png',
];
var i=0;
setInterval(函数(){
var指数=i%照片长度;
i++;
//在每个元素上循环
元素。每个(功能(j){
//现在j是循环的索引
//如果值高于照片,请单击
如果(索引+j>photos.length-1)索引=0-j;
//设置src
$(this.attr('src',photos[index+j]);
})
},2000)
})


请添加完整的代码(包括HTMLY),因为您没有退出条件,所以它将一直运行,直到崩溃。我认为您应该签出setInterval方法,因为您希望在间隔内执行某些操作。将setTimeout(carousel(n),5000)更改为setTimeout(carousel,5000,n)。如果您已经使用引导,为什么不使用它们?如果
n
已经被使用,您将无条件地永远调用相同的函数,而不会提前返回,因此我猜您实际需要做的是编写此
setTimeout(carousel,5000,n)
这样计时器将在5秒内用该
n
值调用函数。@Boandry没问题。使用任何适合您需要的工具:)@Abdul Sadik Yalcin,对不起,您的实现看起来更干净了)Thnaks,很抱歉浪费了时间)