jquery - 如何为这个特定示例设置加载 Jquery 的超时?

标签 jquery javascript jquery-load

如何为这个使用字符串传递数据的特殊示例设置加载超时?看起来应该从 compare_proc.php api 加载的谷歌图表需要很长时间......因为在点击事件中,我得到一个空白结果,我期待从 compare.php 加载图表......我会感谢我如何为这个特定的 Jquery 操作设置超时。谢谢!

    jQuery(document).ready(function() {
          $("#opposition a").click(function(e) {
          var first_id  = $(this).attr('id');
          var second_id = $("h1").attr('id');
          $("div#test").load('compare_proc.php','id=' + first_id + '&id2=' + second_id);
          e.preventDefault();
            });
});

最佳答案

jQuery load() 方法不接受任何 settings 参数。所以你必须用更底层的方法替换它 ajax() call - 允许您为此特定请求指定 timeout 设置。一种可能的方法:

$.ajax('compare_proc.php', {
  data: 'id=' + first_id + '&id2=' + second_id,
  timeout: someTimeoutInMs,
  success: function(resp) {
    $('#test').html(resp);
  }
});

另一种方法是保持代码不变,但使用 ajaxSetup() 修改全局 AJAX 设置调用(在调用 load 之前)。请注意,在其文档页面上强烈建议不要使用此 API:

The settings specified here will affect all calls to $.ajax or AJAX-based derivatives such as $.get(). This can cause undesirable behavior since other callers (for example, plugins) may be expecting the normal default settings. For that reason we strongly recommend against using this API. Instead, set the options explicitly in the call or define a simple plugin to do so.

不过,在您的情况下(仅更改超时设置时)也可以采用这种方式。

关于jquery - 如何为这个特定示例设置加载 Jquery 的超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16129292/

相关文章:

javascript - 如何将 CSS 应用于除悬停元素之外的其他元素?

javascript - 显示更多链接未检测字符限制

jquery - 如何访问显示为 none 的元素并更改其 css 属性

ajax - 从 PHP 文件加载 HTML 时,jQuery.load 或 jQuery.ajax() 哪个更快?

javascript - 检测浏览器是否禁用图像

javascript - jQuery 触发器 ('click' ) 不适用于 Jasmine-jquery

javascript - 下一个类的 sibling 不工作

javascript - AngularJS 中的链接选项

javascript - 在使用 jQuery 加载图像之前显示加载图像?

javascript - 通过 AJAX 加载 SPA 网页