简体   繁体   English

推迟一个ajax调用

[英]deferring an ajax call

I have a bit of js code on one of my pages that uses ajax to load some page elements. 我的一个页面上有一些js代码,这些代码使用ajax加载了一些页面元素。 These happen at the very top of the page and unfortunately slow down loading of the rest of the page. 这些发生在页面的顶部,不幸的是减慢了页面其余部分的加载速度。 I was wondering if there is a way to defer this so it loads after the page has loaded. 我想知道是否有一种方法可以延迟此操作,以便在页面加载后加载它。

Here is the code I am using and it is located right before the closing body tag: 这是我正在使用的代码,它位于结束body标签之前:

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();

As suggested by @KamalSingh, 正如@KamalSingh所建议的那样,

 $(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(); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM