請在我的評論剛剛位於location.href = url下面之後建議延遲5秒的方式;我的評論後需要延遲
我已經嘗試過setTimeout和setInterval,但它在所有瀏覽器中表現不正確。 我也曾嘗試睡眠爲實現如下:
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
console.log(new Date());
console.log('Deleting AdHoc...');
sleep(6000);
console.log(new Date());
但這也並不總是和工作也沒有優雅。
function xToolbar(url, target, name, options, message, height, width) {
if (message == '') {
ok = true
} else {
ok = confirm(message);
}
if (ok == true) {
if (target == 'window') {
if (height != '') {
myTop = (window.screen.height/2) - ((height/2) + 10);
myLeft = (window.screen.width/2) - ((width/2) + 10);
options = 'height=' + height + ',width=' + width + ',top=' + myTop + ',left=' + myLeft + ',' + options;
}
var popup = window.open('', "Loader", options);
var baseUrls = location.href.split("/")
var baseUrl = baseUrls[0] + "/" + baseUrls[1] + "/" + baseUrls[2] + "/" + baseUrls[3] + "/" + baseUrls[4]
popup.document.writeln('<!--fix for ie7-->')
popup.document.writeln('<!DOCTYPE html>')
popup.document.writeln('<html><head><style>')
popup.document.writeln('body, html {width:100%;height:100%;margin:0;padding:0;background:#fff;color:#333;font:Normal 12px Arial, Helvetica, Sans-Serif;}')
popup.document.writeln('h5 {font-weight:normal; font-size:14px;margin:0 0 10px 0;}')
popup.document.writeln('#outer {width:100%;height:90%;position:relative;}')
popup.document.writeln('#outer[id]{display:table;position: static;}')
popup.document.writeln('#middle {position:absolute;top:50%;width: 100%;text-align:center;}')
popup.document.writeln('#middle[id] {display: table-cell;vertical-align: middle;width: 100%;position: static;}')
popup.document.writeln('#inner {position:relative;top:-50%}')
popup.document.writeln('</style></head><body>')
popup.document.writeln('<div id="outer"><div id="middle"><div id="inner"><h5>Processing your request</h5>')
popup.document.writeln('<img src="' + baseUrl + '/images/toolbar/loader_fan.gif" id="loader" border="0" style="vertical-align:middle"/></div></div></div>')
popup.document.writeln('<script type="text/javascript">')
popup.document.writeln('function load(){')
popup.document.writeln('location.href = "' + baseUrl + "/" + url.replace(/\\/g, '\\\\') + '";')
popup.document.writeln('loader.setAttribute("src","' + baseUrl + '/images/toolbar/loader_fan.gif");};')
popup.document.writeln('</script>')
popup.document.writeln('</body></html>')
popup.document.close();
popup.load();
} else {
if (target == 'frame') {
url += '{amp}EXECUTE_TEMPLATE=$';
var frame = document.getElementById(name);
frame.src = url;
} else {
location.href = url;
// Comment : Please put here some code to delay by 5 seconds
}
}
}
}
在此先感謝。
請在設置定時器新的location.href? –
設置一個location.href後,頁面會發生變化,所以不管你給什麼超時,它都不會執行,因爲代碼不再存在。或者你有意推遲位置更改? –
'document.writeln'使用'document.write',應該避免(參見[規範]中的警告(http://www.w3.org/TR/html5/dom.html#document.write%28 %29))。改用DOM方法。 – Oriol