2016-11-30 35 views
-2

我正在使用函數pinterest_it()來調整視圖與masonry.js,我正在使用另一個函數recipeLoader()顯示加載圖標,直到頁面完全加載,然後顯示內容。這適用於頁面刷新,但是當我使用ajax加載內容時,我的$(window).load(...)函數永遠不會觸發。jQuery:ajax完成渲染後的調用函數

怎樣才能使

function pinterest_it() { 
    $(".loader").show(); # shows loading icon 
    $("#recipe-index").css("opacity", 0); # makes content invisible 

    if ($(document).innerWidth() < 991) { 
     gutter = 0 
    } else { 
     gutter = 16 
    } 

    var $grid = $('.grid').imagesLoaded(function() { 

     var $blah = $('.grid').masonry({ 
      itemSelector: '.grid-item', 
      columnWidth: function(containerWidth) { 
       return $('.grid-sizer').width(); 
      }, 
      percentPosition: true, 
      gutterWidth: gutter 
     }); 

    }, fixGrid()) 

    recipeLoader(); # see below 
} 

function recipeLoader() { 
    console.log("recipe loader") 
    $(window).load(function() { # doesn't fire after Ajax completes! 
     console.log("loaded") 
     $(".loader").hide(); # hides loading icon 
     $("#recipe-index").animate({ 
      opacity: 1 # shows content 
     }, 400); 
    }); 
} 

# one of the Ajax function that calls pinterest_it() 
$(".togglebutton input").click(function() { 
    console.log($(this).serialize()); 
    $.get(this.action, $('#recipes_search').serialize(), function() { 
     pinterest_it(); 
    }, 'script') 
}); 

我已經試過$(x).ready(...)沒有運氣,以及ajaxComplete()一切完全加載後的函數調用(不。就緒)。也許有一種方法使用$(x).load(...)與一個特定的元素,而不是窗口,以允許該功能觸發?我試過在$("#recipe-index")上調用它,但那也不起作用。

回答

-2

$(window).load在成功/完成Ajax請求時不會觸發。此舉有點自己的功能:

function handlePageRefresh() { 
    console.log("loaded") 
    $(".loader").hide(); # hides loading icon 
    $("#recipe-index").animate({ 
     opacity: 1 # shows content 
    }, 400); 
} 

然後調用它在$(窗口).load &阿賈克斯成功回調

$(window).load(function() { # doesn't fire after Ajax completes! 
    handlePageRefresh() 
}); 

# one of the Ajax function that calls pinterest_it() 
$(".togglebutton input").click(function() { 
    console.log($(this).serialize()); 
    $.get(this.action, $('#recipes_search').serialize(), function() { 
     handlePageRefresh() 
     pinterest_it(); 
    }, 'script') 
}); 
+0

這仍然不起作用,因爲沒有時之間的延遲函數調用完成並且窗口滿載時。 –

+0

看着我的控制檯,我可以看到有一個完整的200請求。當完成時,我想調用我的函數(在你的情況下,handlePageRefresh()) –