如何为此特定示例在加载Jquery中设置超时? [英] How do I set a timeout in load Jquery for this particular example?

查看:109
本文介绍了如何为此特定示例在加载Jquery中设置超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个非常特殊的示例中,如何通过字符串传递数据,我该如何设置加载超时?看来应该从compare_proc.php api加载的Google图表花了很长时间...因为在click事件上,我得到了一个空白结果,并且我希望从compare.php加载图表.感谢我如何为此特定的Jquery操作设置超时.谢谢!

How do I set a timeout in load for this very particular example where I pass data with a string ? It looks like the google charts supposed to be loaded from compare_proc.php api are taking long ... because on the click event , I get a blank result and I'm expecting charts to be loaded from compare.php .. I'd appreciate how I can set a timeout for this particular Jquery operation. Thanks !

    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() 呼叫-可让您为此特定请求指定timeout设置.一种可行的方式:

jQuery load() method doesn't accept any settings param. So you have to replace it with more low-level method ajax() call - which lets you specify timeout settings for this specific request. One possible way to do it:

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

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

The alternative is to leave your code as is, but modify global AJAX settings instead with ajaxSetup() call (prior to calling load). Note that using this API is strongly discouraged on its very documentation page:

此处指定的设置将影响对$.ajax所有调用或 基于AJAX的衍生物,例如$.get().这可能会导致不良后果 行为,因为其他调用者(例如插件)可能正在等待 正常的默认设置.因此,我们强烈建议 反对使用此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.

不过,就您而言(仅更改超时设置),也可以这样做.

Still, in your case (when only timeout settings are changed) it may be ok to go that way as well.

这篇关于如何为此特定示例在加载Jquery中设置超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆