推迟ajax电话

时间:2017-11-14 09:27:22

标签: javascript ajax

我的一个页面上有一些js代码,它们使用ajax加载一些页面元素。这些发生在页面的最顶层,不幸的是减慢了页面其余部分的加载速度。我想知道是否有办法推迟这个,所以它在页面加载后加载。

以下是我正在使用的代码,它位于关闭正文标记之前:

function countrySnippet() {
      $.ajax({
          url: '/country_snippets.php?sites=<?php echo json_encode($jam_sites); ?>&disabled=<?php echo json_encode($array_disabled); ?>&no_cat=<?php echo json_encode($category_disabled); ?>&hide=<?php echo $hide_overlay; ?>&use_id=<?php echo $use_id; ?>&snippet=<?php echo $display_country_snippet; ?>&title_top=<?php echo $snippet_title_top; ?>&title_bottom=<?php echo $snippet_title_bottom; ?>&theme=<?php echo $theme_detail['
          select_theme ']; ?>&code=<?php echo $country_code; ?>&region=<?php echo $country_region; ?>&thumbs=<?php echo $snippet_number_thumbs; ?>&size=<?php echo $settings_detail['
          index_thumbs_size ']; ?>',
          type: "get",
          beforeSend: function() {
            $('.ajax-load-snippet').show();
          }
        })
        .done(function(data) {
          $('.ajax-load-snippet').hide();
          $("#country-snippet").append(data).hide().fadeIn(1000);
        })
        .fail(function(jqXHR, ajaxOptions, thrownError) {
          $('.ajax-load-snippet').hide();
        });
    }
    countrySnippet();

1 个答案:

答案 0 :(得分:1)

正如@KamalSingh所说,

&#13;
&#13;
$(document).ready(function() {
  function countrySnippet() {
    $.ajax({
        url: '/country_snippets.php?sites=<?php echo json_encode($jam_sites); ?>&disabled=<?php echo json_encode($array_disabled); ?>&no_cat=<?php echo json_encode($category_disabled); ?>&hide=<?php echo $hide_overlay; ?>&use_id=<?php echo $use_id; ?>&snippet=<?php echo $display_country_snippet; ?>&title_top=<?php echo $snippet_title_top; ?>&title_bottom=<?php echo $snippet_title_bottom; ?>&theme=<?php echo $theme_detail['
        select_theme ']; ?>&code=<?php echo $country_code; ?>&region=<?php echo $country_region; ?>&thumbs=<?php echo $snippet_number_thumbs; ?>&size=<?php echo $settings_detail['
        index_thumbs_size ']; ?>',
        type: "get",
        beforeSend: function() {
          $('.ajax-load-snippet').show();
        }
      })
      .done(function(data) {
        $('.ajax-load-snippet').hide();
        $("#country-snippet").append(data).hide().fadeIn(1000);
      })
      .fail(function(jqXHR, ajaxOptions, thrownError) {
        $('.ajax-load-snippet').hide();
      });
  }
  countrySnippet();
});
&#13;
&#13;
&#13;