我的评论后需要延迟

时间:2014-04-10 18:40:44

标签: javascript jquery

请在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
        }
    }
}

}

提前致谢。

2 个答案:

答案 0 :(得分: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);
      }
   }

答案 1 :(得分:0)

如果希望在给定延迟后加载页面,请在location.href链接之前使用setTimeout()。正如帕特里克所提到的,它将在转到其他页面后执行。

相关问题