简体   繁体   English

去掉 <div> 超时后由Javascript添加

[英]Remove <div> added by Javascript after timeout

I have gone through a number of examples on SO (including this one and this one ) to try and resolve an issue I'm having. 我在SO上经历了许多示例(包括例和本例 ),以尝试解决我遇到的问题。

Using the script below I am adding an overlay to the page with a 'spinning' image to indicate to the user that the system is busy processing after they clicked the button. 使用下面的脚本,我在页面上添加了带有“旋转”图像的叠加层,以向用户指示用户单击按钮后系统正在忙于处理。

ACME.global = ACME.global ? ACME.global : {
    init: function() {
        $(".ctaButton").bind("click", this.pageLoadingOverlay);
    },
    pageLoadingOverlay : function(event) {
        var body = document.getElementsByTagName("body");
        var pageOverlay = document.createElement("div");
        pageOverlay.id = "onClickPageOverlay";
        body[0].appendChild(pageOverlay);
        setTimeout(function() {
            $('#onClickPageOverlay').remove();
        }, 1000);
    }
}

If I leave out the timeout functionality 如果我没有超时功能

        setTimeout(function() {
            $('#onClickPageOverlay').remove();
        }, 30000);

The click event is properly bound to the button and the overlay is loaded. click事件已正确绑定到按钮,并且叠加层已加载。 The problem is that the UI could then get stuck on the overlay display (the system rendering the page does not have a "refresh" or "back" button so the user can't reload the page). 问题在于UI可能会卡在叠加显示上(呈现页面的系统没有“刷新”或“后退”按钮,因此用户无法重新加载页面)。

However adding the full script it appears that the onClick event is no longer bound to the script. 但是,添加完整的脚本后,似乎onClick事件不再绑定到脚本。

I've tried various combinations of setTimeout and delay with no luck. 我尝试了setTimeoutdelay各种组合,但没有运气。

Any help greatly appreciated. 任何帮助,不胜感激。

The timeout function isnt´t well build. 超时功能构建得不好。

Try this: 尝试这个:

var t=setTimeout("$('#onClickPageOverlay').remove()",30000);

Documentation: 说明文件:

setTimeout("javascript function",milliseconds);

The setTimeout() method will wait the specified number of milliseconds, and then execute the specified function. setTimeout()方法将等待指定的毫秒数,然后执行指定的函数。

The first parameter of setTimeout() should be a function. setTimeout()的第一个参数应该是一个函数。

The second parameter indicates how many milliseconds, from now, you want to execute the first parameter. 第二个参数指示从现在开始要执行多少毫秒。

正如我已经在评论中指出的那样,该代码可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM