Javascript 在我的评论之后需要延期吗

Javascript 在我的评论之后需要延期吗,javascript,jquery,Javascript,Jquery,请建议我在location.href=url下方发表评论后延迟5秒的方式 我尝试过setTimeout和setInterval,但它在所有浏览器中都不能正常运行。 我还尝试了睡眠,如下所示: function sleep(milliseconds) { var start = new Date().getTime(); for (var i = 0; i < 1e7; i++) {

请建议我在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());
函数睡眠(毫秒){
var start=new Date().getTime();
对于(变量i=0;i<1e7;i++){
如果((新日期().getTime()-start)>毫秒){
打破
}
}
}
console.log(新日期());
log('Deleting AdHoc…');
睡眠(6000);
console.log(新日期());
但这也不总是有效,也不优雅

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
        }
    }
}
函数xToolbar(url、目标、名称、选项、消息、高度、宽度){
如果(消息==''){
ok=正确
}否则{
确定=确认(消息);
}
如果(ok==真){
如果(目标=='窗口'){
如果(高度!=''){
myTop=(window.screen.height/2)-(height/2)+10;
myLeft=(window.screen.width/2)-(width/2)+10;
选项='height='+height+'、width='+width+'、top='+myTop+'、left='+myLeft+'、'+options;
}
var popup=window.open(“”,“加载程序”,选项);
var baseUrls=location.href.split(“/”)
var baseUrl=baseUrls[0]+“/”+baseUrls[1]+“/”+baseUrls[2]+“/”+baseUrls[3]+“/”+baseUrls[4]
popup.document.writeln(“”)
popup.document.writeln(“”)
popup.document.writeln(“”)
popup.document.writeln('正文,html{宽度:100%;高度:100%;边距:0;填充:0;背景:fff;颜色:333;字体:普通12px Arial,Helvetica,无衬线;}'))
popup.document.writeln('h5{字体大小:正常;字体大小:14px;边距:0 10px 0;}'))
popup.document.writeln('#外部{宽度:100%;高度:90%;位置:相对;}'))
popup.document.writeln('#outer[id]{display:table;position:static;}'))
popup.document.writeln(“#中间{位置:绝对;顶部:50%;宽度:100%;文本对齐:中间;}”)
popup.document.writeln('#middle[id]{显示:表格单元格;垂直对齐:中间;宽度:100%;位置:static;}'))
popup.document.writeln(“#内部{位置:相对;顶部:-50%}”)
popup.document.writeln(“”)
popup.document.writeln('处理您的请求')
popup.document.writeln(“”)
popup.document.writeln(“”)
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(“”)
popup.document.writeln(“”)
popup.document.close();
popup.load();
}否则{
如果(目标=='frame'){
url+='{amp}执行_模板=$';
var frame=document.getElementById(名称);
frame.src=url;
}否则{
location.href=url;
//注释:请在这里输入一些代码以延迟5秒
}
}
}
}


提前感谢。

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

如果您想延迟页面更改,请使用setTimeout,您说您已经尝试使用setTimeout,但如果您没有向我们展示您是如何使用它的,我将假设您使用它是错误的

...
else {
   setTimeout(function(){
      window.location.href=url;
   },5000);
}
如果您需要刷新opener页面,并且如果弹出窗口属于同一域,则可以在容器窗口上设置计时器

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

是否要延迟注释,或延迟转到新位置.href?设置location.href后,页面将发生更改,因此无论您给出什么超时,它都不会执行,因为代码不再存在。或者您想延迟位置更改吗?
文档。writeln
使用
文档。write
,应避免使用(请参阅中的警告)。改为使用DOM方法。您是如何使用setTimeout的?@PatrickEvans我必须关闭页面并从打开新窗口/选项卡的位置刷新页面…我需要在setTimeout中调用window.close()和window.opener.location.href=window.opener.location.href。您可以编写代码段吗?您可以调用另一个函数,如setTimeout(closeandopenwindow(),6000);并将代码分别包含在closeandopenwindow()函数中。