目前在用google map的api,现在不能加载成功api的js文件,页面打开后会一直显示加载状态,有没有什么方法停止继续加载这个js文件?
现在的需求是如果不能加载成功google map api,需要进行其他的操作。
试了一下window.stop()
,这样不行,会把其他需要加载的也停止了。
谢谢~
这是个好问题。且不说权力机关或ISP企业侵害、污染、阻断合法网络通讯内容的客观事实,就算只是偶然的通讯故障,对用户体验的伤害也都是等同的。
<script>
的加载总是同步(阻塞性)的,也不能用DOM操作去影响。题主需要的是独立于页面加载与渲染的异步JS加载。工具有很多,这里举一个RequireJS的例子:
HTML页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test Page</title>
<script src="//cdn.staticfile.org/require.js/2.1.15/require.min.js" data-main="test1"></script>
</head>
<body></body>
</html>
保存为test1.js
:
require.config({
paths: {
'jquery': '//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery',
'underscore': '//cdn.bootcss.com/underscore.js/1.7.0/underscore'
},
waitSeconds: 20
});
require(['jquery'], function (module) {
console.log("jQuery " + $.fn.jquery + " successfully loaded. ");
}, function (err) {
console.log("SHIT happened while loading jQuery! ");
});
require(['underscore'], function (module) {
console.log(_.last([1, 2, 3, "Underscore.js successfully loaded. "]));
}, function (err) {
console.log("SHIT happened while loading Underscore.js! ");
});
timeout:500 //定时,当多少秒后加载没完成取消加载。