有没有办法取消/停止/中止getScript调用? [英] Is there any way to cancel/stop/abort a getScript call?

查看:57
本文介绍了有没有办法取消/停止/中止getScript调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直在寻找并且似乎没有办法实际中止/取消/停止脚本调用。

So, I've been looking about and there doesn't appear to be a way to actually abort/cancel/stop a script call once its made.

我发现必须使用延迟加载来解决对第三方有点奇怪的无响应脚本调用。使用json / ajax,确定我可以暂停它 - 太棒了。但是通过脚本调用,没有这样的运气。我认为jQuerys $ .getScript会允许这样的行为。不?

I find having to use lazy load to address a non-responsive script call to a third party kinda odd. With json/ajax, sure I can just timeout on it - great. But with a script call, no such luck. I figured jQuerys $.getScript would allow for such behavior. no?

我希望完成的任务:取消阻止js通话。

What I am hoping to accomplish: cancel a blocking js call.

不能像这样的工作吗?

var getScript = $.getScript( "ajax/test.js", function( data, textStatus, jqxhr ) {
     //
});

var exitOut = setTimeout(function(){
    getScript.abort();
},2000)

从我读过的脚本请求中,不能在中间步骤中止。

from what I've been reading a "script" request cannot be abort mid-stride.

但是,因为getScript只是一个ajax调用,所以我希望超时也适用于此处。但是我的一些测试并没有说明这一点?

BUT, since getScript is just an ajax call, I was hoping that timeout could also apply here. But some of my tests aren't bearing that out?

除了延迟加载之外的任何其他解决方案?

Any other solutions besides lazy loading?

推荐答案


但是,因为getScript只是一个ajax调用,所以我希望超时也适用于此。

BUT, since getScript is just an ajax call, I was hoping that timeout could also apply here.

实际上, getScript 不使用XHR但<$ code>< script> 元素。因此, abort 不起作用,超时 ajax 选项都不会。

Actually, getScript does not use XHR but a <script> element. Therefore, abort does not work and the timeout ajax option does neither.

你可能最好通过XHR加载脚本,然后手动评估它:

You might be better off with loading the script via XHR and then manually evaling it:

$.ajax({
    url: "ajax/test.js",
    dataType: "text",
    timeout: 2000
}).done(function(str) {
    $.globalEval(str);
});

这篇关于有没有办法取消/停止/中止getScript调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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