javascript - jquery延迟函数与IF语句

标签 javascript jquery arrays

谁能告诉我为什么我的 IF 语句在使用每个循环更新 UI 之前会触发?

该代码基本上是想延迟向 UI 添加 css 类,然后在添加每个类后重定向用户。目前只是立即指挥?!

$("#logo").click(function() {

    //define variables:
    var eventDuration = 500;
    var elementArray = ['ribbon', 'left-panel', 'wid-id-1', 'wid-id-2'];
    var animationArray = ['slideOutRight', 'slideOutLeft', 'rotateOutUpRight', 'rotateOutUpRight'];

    //Loop through elements and update UI with timer function:
    $.each(elementArray, function(index, value) {

        //Increments the delay of the element updates:
        var delaytimer = index * eventDuration + eventDuration;

        //Adds animation css classes to onpage elements:
        $('#' + value).delay(delaytimer).queue(function() {
            $(this).addClass('animated ' + animationArray[index]).dequeue();
        });

        //Once complete redirect to the home page:
        if (index === 3) {
            $(this).delay(delaytimer + 500).queue(function() {
                window.location.replace('/').dequeue;
            });
        }

    });
});

最佳答案

您的 if 语句将立即执行,因为它不在延迟函数内。尝试将其移到那里。

$('#' + value).delay(delaytimer).queue(function() {
        $(this).addClass('animated ' + animationArray[index]).dequeue();


       //Once complete redirect to the home page:
       if (index === 3) {
            $(this).delay(delaytimer + 500).queue(function() {
               window.location.replace('/').dequeue;
           });
       }
    });

关于javascript - jquery延迟函数与IF语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23827750/

相关文章:

javascript - BootStrap 模态不想关闭

javascript - 本地存储与 Web SQL

c# - 如何通过 jQuery ajax 和 C# 下载文件

php - 在 jQuery 输出中递增 "Name"

php - JavaScript 的非浏览器模拟——这可能吗?

javascript - 在交互式报表上获取行数据

javascript - 如何使用jquery检查某个元素是否在用户 View 中

JavaScript:如何将数组值与每个匹配项匹配,而不是与所有匹配项匹配

java - 迭代线程之间共享的数组

javascript - 如何分割数组或分配键,jquery