JS倒计时似乎运行了两次



我在我的网站上运行以下功能。它作为一个基本定时器工作,一旦i = 0,它应该继续执行else语句。然后,它运行一个AJAX GET并将一个数字输出到$assetID。我的问题是,这似乎运行两次。我本该只收到一个提醒,却收到了两个。你知道我做错了什么吗?

var i = 3;
function timer() {
    if (i <= 3 && i > 0) {
        $('#count').fadeIn();
        $('#count').attr('src', 'images/'+i+'.png');
        setTimeout(timer, 1000);
        i--;
    } else {
            //var intProcessingTimeout = 0;
            $.ajax({
                type: "GET",
                url: "index_ajax.php",
                data: "action=submit_info",
                complete: function(data){
                        //console.log(data);
                        i = 3;
                        $assetID = data.responseText.replace(/ /g,'');
                        //$assetID = $.trim($(this).text());
                        alert($assetID);

                        $('#get-ready, #count, #swf_file, #back').fadeOut('slow', function() {
                            $('#preview-poster').fadeIn();
                            $('.your_poster').fadeIn();
                            checkAssetStatusNew();
                        });

                }
          });
    }
}

timer()在这里被调用:

$(".take_photo").click(function () {

    $('#preview-page, #back').fadeOut('slow', function() {
          $('#get-ready').fadeIn();
          $('#swf_file').fadeIn();
          //setTimeout(timer, 1000); //DOESNT WORK HERE - RUNS TWICE
    });
         setTimeout(timer, 1000); // WORKS HERE FOR SOME REASON

});

编辑:将setTimeout(timer, 1000);移出淡出函数似乎可以使其正常工作。不知道为什么在fadeOut函数内会运行两次

定时器块工作正常…试过日志…

Timer start...
If block...
Timer start...
If block...
Timer start...
If block...
Timer start...
Else block... 

完全符合预期。

尝试替换

complete: function(data){...}

success: function(data){...},
error: function(err){...)

最新更新