如何一个接一个地延迟调用javascript函数

How to call the javascript function one after the other with delay?

本文关键字:延迟 调用 javascript 函数 一个 何一个      更新时间:2023-09-26

这里有一个JavaScript函数,它遍历数组并将图像附加到div。当到达数组中的最后一个元素时,该函数调用flip函数来缩放div,3秒后该函数重复。即,在3秒钟内,我们可以看到翻转的内容,3秒钟后,它再次从数组迭代中继续。。。。

我有6组相同的函数,如下所示。。。例如翻转1、翻转2、翻转3等等,直到翻转6。现在我想做的是,我想在flip-1完成3秒的翻转后立即调用flip-5。。。。我该怎么做?

function startSlidecat1(started) {
  for (var i = 0; i < footwear.length; i++) {
    var image = footwear[i][0];
    imgslidercat1(image, i / initial_del * 8000, i == footwear.length - 1);
  }
};
function imgslidercat1(image, timeout, last) {
  window.setTimeout(function() {
    document.getElementById('flip-1').style.display = 'none';
    document.getElementById('category-1').style.display = 'block';
    document.getElementById('category-1').innerHTML = "";
    var product = document.getElementById('category-1');
    var elem = document.createElement("img");
    product.appendChild(elem);
    elem.src = image;
    if (last) {            
        flip1();
    }
  }, timeout);
}
startSlidecat1();
function flip1(){    
    $('#category-1').css('display', 'none');    
    $('.box-1').delay(100).css('display', 'block');
    //$('#category-1').delay(100).addClass("closeover"); 
    $('.box-1').addClass("opensesame");  
    setTimeout(closeIt, delay_time);
    //flip2();
  }
function closeIt(){
   // $('.box-1').addClass("closesesame");
    //$('.box-1').removeClass("closesesame");     
    $('#category-1').addClass("opensesame");
    setTimeout(startSlidecat1, 400);     
}
setTimeout(flip5,3000);

这将在3秒(=3000ms)后调用"flip5"函数。