在 WebKit 浏览器上,您可以使用 window.stop() 取消浏览器正在加载的所有内容。这将导致浏览器终止对下一页的请求(以及任何其他下载图像、javascript、ajax 等的请求)
如果页面最终通过设计加载,我解决了连接缓慢对话框消失的问题。我没有使用对话框,而是将其设置为覆盖在页面中间的功能区,因此当页面响应最终通过时,如果功能区和页面消失,对用户来说就不那么刺耳了。下面的等待选项只会使功能区消失。以下是适用于 Webkit 浏览器的代码:
// This code executes in the click event for a link to forUrl
clearTimeout(MyApp.slowPageTimeout);
MyApp.slowPageTimeout = setTimeout('MyApp.slowPageLoadEvent("'+forUrl+'");', 10000);
slowPageLoadEvent: function(forUrl) {
var cachedDocument = myApp.getDocumentByUrl(forUrl);
var height = 100;
var position = window.innerHeight/2+window.scrollY-height/2;
var slowHttp = "<div id='slow-http-warning'>Slow connection. <span id='no-wait'>Read offline</span>, or <span id='wait'>wait</span>?</div>";
if ($("#slow-http-warning").length == 0) {
$('body').append(slowHttp);
}
else {
$('#slow-http-warning').html(slowHttp);
}
$("#slow-http-warning").css('top', ""+position+"px");
$("#wait").click(function() {
$('#slow-http-warning').remove();
});
$("#no-wait").click(function() {
window.stop(); // not supported in IE, but it has an equivalent: document.execCommand("Stop");
$('#slow-http-warning').remove();
MyApp.setOffline(); // switch to operating in offline mode
MyApp.go(cachedDocument); // display page from local storage cache
return;
});
}