2014-04-10 61 views
0

請在我的評論剛剛位於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 
     } 
    } 
} 

}

在此先感謝。

+0

請在設置定時器新的location.href? –

+1

設置一個location.href後,頁面會發生變化,所以不管你給什麼超時,它都不會執行,因爲代碼不再存在。或者你有意推遲位置更改? –

+0

'document.writeln'使用'document.write',應該避免(參見[規範]中的警告(http://www.w3.org/TR/html5/dom.html#document.write%28 %29))。改用DOM方法。 – Oriol

回答

0

如果您希望頁面在給定延遲後加載,請在location.href鏈接之前使用setTimeout()。正如帕特里克所提到的,它在進入其他頁面後不會被執行。

+0

我需要在setTimeout中調用window.close()和window.opener.location.href = window.opener.location.href。你能否寫下這段代碼? – apratik

+0

你可以調用另一個函數,如setTimeout(closeandopenwindow(),6000);並分別將你的代碼包含在closeandopenwindow()函數中。 – Sri

1

,如果你是想拖延頁面的變化,然後使用setTimeout的,你說你已經使用setTimeout的嘗試,但沒有你向我們展示你如何使用它,我將假設你用錯了

... 
else { 
    setTimeout(function(){ 
     window.location.href=url; 
    },5000); 
} 

如果您需要刷新揭幕戰網頁,如果彈出的是同一個域,那麼你可以在容器窗口要推遲評論,或推遲要去

... 
    else { 
     var opener = window.opener; 
     if(opener !== null){ 
     opener.setTimeout(function(){ 
      opener.location.href=url; 
     },5000); 
     } 
    }