如何为这个使用字符串传递数据的特殊示例设置加载超时?看起来应该从 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/