Javascript 为什么要执行两次?

Javascript 为什么要执行两次?,javascript,jquery,firefox,execution,Javascript,Jquery,Firefox,Execution,我有这样的代码: $(document).ready(function() { $("div #covert, div #coverb").height($(window).height() / 2 + 1); $(window).resize(function() { $("div #covert, div #coverb").height($(window).height() / 2 + 1); covconcr(); });

我有这样的代码:

$(document).ready(function() {

    $("div #covert, div #coverb").height($(window).height() / 2 + 1);
    $(window).resize(function() {
        $("div #covert, div #coverb").height($(window).height() / 2 + 1);
        covconcr();
    });

    function covconcr() {
        $('div #covercon').css('left', $(window).width() / 2 - $('#covercon').width() / 2);
    }

    covconcr();

    function hidecover() {
        var goup = $('div #covert').height();
    }

    $("div #covercon").fadeOut("fast").fadeIn("fast").fadeOut("fast").fadeIn("fast").fadeOut("fast").fadeIn("fast").fadeOut("fast").fadeIn("fast").fadeOut("fast").delay(100).fadeIn("fast", function() {
        $(this).stop();
    });

    $('title').html('Drink86_browser.detection');

    var logoop;

    jQuery.each(jQuery.browser, function() {
        if ($.browser.msie) {
            $('div #covercon').delay(3000, function() {
                $(this).html("YOUR BROWSER IS: INTERNET EXPLORER.");
                $('title').html('Drink86_your.browser.is.internet.explorer');
            });
        }
        else if (!$.browser.msie) {
            function update() {
                //$('#site').load('site.php');
                $('div #covercon').html("YOUR BROWSER IS: " + jQuery.uaMatch(navigator.userAgent).browser + ".");
                covconcr();
                $('title').html('Drink86_your.browser.is.' + jQuery.uaMatch(navigator.userAgent).browser + ' ');

                function hidecov() {
                    $('title').html('Drink86_loading_files');
                    $('#covercon').html($('#loading').html());
                    covconcr();
                    var timer = setInterval(function() {
                        $("#loadingpro").html(Math.round($("#progress").width() / 4) + "%");
                    }, 20);
                    $("#progress").animate({
                        width: 400
                    }, 2000, function() {
                        $('title').html('Drink86_');
                        clearInterval(timer);
                        $('#covercon').delay(700).fadeOut('fast', function() {
                            //    if(logoop!="yes"){
                            $('#logobig').css('left', $(window).width() / 2 - $('#logobig').width() / 2).css('top', $(window).height() / 2 - $('#logobig').height() / 2);
                            $('#logobig').fadeIn(3000).delay(2500).fadeOut(3000);
                            logoop = "yes";
                            // }
                        });
                    });
                }
                setTimeout(hidecov, 1000);
            }
            setTimeout(update, 3100);
        }
    });
});
if ($.browser.msie) {
    $('div #covercon').delay(3000, function() {
        $(this).html("YOUR BROWSER IS: INTERNET EXPLORER.");
        $('title').html('Drink86_your.browser.is.internet.explorer');
    });
} else if (!$.browser.msie) {
    function update() {
        //$('#site').load('site.php');
        $('div #covercon').html("YOUR BROWSER IS: " + jQuery.uaMatch(navigator.userAgent).browser + ".");
        covconcr();
        $('title').html('Drink86_your.browser.is.' + jQuery.uaMatch(navigator.userAgent).browser + ' ');

        function hidecov() {
            $('title').html('Drink86_loading_files');
            $('#covercon').html($('#loading').html());
            covconcr();
            var timer = setInterval(function() {
                $("#loadingpro").html(Math.round($("#progress").width() / 4) + "%");
            }, 20);
            $("#progress").animate({
                width: 400
            }, 2000, function() {
                $('title').html('Drink86_');
                clearInterval(timer);
                $('#covercon').delay(700).fadeOut('fast', function() {
                    //    if(logoop!="yes"){
                    $('#logobig').css('left', $(window).width() / 2 - $('#logobig').width() / 2).css('top', $(window).height() / 2 - $('#logobig').height() / 2);
                    $('#logobig').fadeIn(3000).delay(2500).fadeOut(3000);
                    logoop = "yes";
                    // }
                });
            });
        }
        setTimeout(hidecov, 1000);
    }
    setTimeout(update, 3100);
}
使用
#logobig
fadeIn
fadeOut
)执行两次操作。 为什么? 我以前的问题与这些问题类似,但仅仅在Firefox中就执行了两次。
知道为什么吗?

你到底在想什么

jQuery.each(jQuery.browser, function() { ...
那真的毫无意义。只要看看
jQuery.browser
(如果必须的话)

此外,检查是否在“如果”语句中是IE,然后在“其他”部分中是否不是IE,好吧,再次


您的问题的直接答案是
jQuery。浏览器中可能有两件事,所以您要做两次。

我认为您的问题是您正在迭代jQuery的浏览器标志集合,这对于完成您正在做的事情是不必要的。如果您在Firefox中查看页面,If/elseif的第二个子句将在每个循环中执行,因为该值永远不会更改。尝试从每个循环中删除该函数,使其看起来如下所示:

$(document).ready(function() {

    $("div #covert, div #coverb").height($(window).height() / 2 + 1);
    $(window).resize(function() {
        $("div #covert, div #coverb").height($(window).height() / 2 + 1);
        covconcr();
    });

    function covconcr() {
        $('div #covercon').css('left', $(window).width() / 2 - $('#covercon').width() / 2);
    }

    covconcr();

    function hidecover() {
        var goup = $('div #covert').height();
    }

    $("div #covercon").fadeOut("fast").fadeIn("fast").fadeOut("fast").fadeIn("fast").fadeOut("fast").fadeIn("fast").fadeOut("fast").fadeIn("fast").fadeOut("fast").delay(100).fadeIn("fast", function() {
        $(this).stop();
    });

    $('title').html('Drink86_browser.detection');

    var logoop;

    jQuery.each(jQuery.browser, function() {
        if ($.browser.msie) {
            $('div #covercon').delay(3000, function() {
                $(this).html("YOUR BROWSER IS: INTERNET EXPLORER.");
                $('title').html('Drink86_your.browser.is.internet.explorer');
            });
        }
        else if (!$.browser.msie) {
            function update() {
                //$('#site').load('site.php');
                $('div #covercon').html("YOUR BROWSER IS: " + jQuery.uaMatch(navigator.userAgent).browser + ".");
                covconcr();
                $('title').html('Drink86_your.browser.is.' + jQuery.uaMatch(navigator.userAgent).browser + ' ');

                function hidecov() {
                    $('title').html('Drink86_loading_files');
                    $('#covercon').html($('#loading').html());
                    covconcr();
                    var timer = setInterval(function() {
                        $("#loadingpro").html(Math.round($("#progress").width() / 4) + "%");
                    }, 20);
                    $("#progress").animate({
                        width: 400
                    }, 2000, function() {
                        $('title').html('Drink86_');
                        clearInterval(timer);
                        $('#covercon').delay(700).fadeOut('fast', function() {
                            //    if(logoop!="yes"){
                            $('#logobig').css('left', $(window).width() / 2 - $('#logobig').width() / 2).css('top', $(window).height() / 2 - $('#logobig').height() / 2);
                            $('#logobig').fadeIn(3000).delay(2500).fadeOut(3000);
                            logoop = "yes";
                            // }
                        });
                    });
                }
                setTimeout(hidecov, 1000);
            }
            setTimeout(update, 3100);
        }
    });
});
if ($.browser.msie) {
    $('div #covercon').delay(3000, function() {
        $(this).html("YOUR BROWSER IS: INTERNET EXPLORER.");
        $('title').html('Drink86_your.browser.is.internet.explorer');
    });
} else if (!$.browser.msie) {
    function update() {
        //$('#site').load('site.php');
        $('div #covercon').html("YOUR BROWSER IS: " + jQuery.uaMatch(navigator.userAgent).browser + ".");
        covconcr();
        $('title').html('Drink86_your.browser.is.' + jQuery.uaMatch(navigator.userAgent).browser + ' ');

        function hidecov() {
            $('title').html('Drink86_loading_files');
            $('#covercon').html($('#loading').html());
            covconcr();
            var timer = setInterval(function() {
                $("#loadingpro").html(Math.round($("#progress").width() / 4) + "%");
            }, 20);
            $("#progress").animate({
                width: 400
            }, 2000, function() {
                $('title').html('Drink86_');
                clearInterval(timer);
                $('#covercon').delay(700).fadeOut('fast', function() {
                    //    if(logoop!="yes"){
                    $('#logobig').css('left', $(window).width() / 2 - $('#logobig').width() / 2).css('top', $(window).height() / 2 - $('#logobig').height() / 2);
                    $('#logobig').fadeIn(3000).delay(2500).fadeOut(3000);
                    logoop = "yes";
                    // }
                });
            });
        }
        setTimeout(hidecov, 1000);
    }
    setTimeout(update, 3100);
}

我不是Javascript的高手,所以我可能对此完全不感兴趣,但看起来hidecov可能在调用update时运行,然后在1秒后再次运行

在hidecov内部使用带有断点的firebug应该指向它运行的时间。

您调用
setTimeout(hidecov,1000)
内部
update()
,然后
setTimeout(update,3100)
外部更新,因此
hidecov
将在1000毫秒后调用一次,然后
update
将在3100毫秒后调用,在那里,它将在1000毫秒内对hidecov执行另一个
setTimeout

我不确定所有这些代码都在试图做什么,所以我无法告诉您如何更改它,但我可以理解为什么会调用两次
hidecov

jQuery.each(jQuery.browser


不要循环浏览“browser”对象中的每一项。我怀疑不止一项是!$.browser.msie

,因为JavaScript是一个很残酷的工具,行前4个空格格式化为代码。
ctr-k
以供选择。您是否尝试过使用Firebug单步浏览JavaScript?这一定是我在一段时间内见过的格式最差的代码乐