使用setTimeout将弹出窗口延迟20秒

Delaying the pop up for 20 seconds with setTimeout

本文关键字:窗口 延迟 20秒 setTimeout 使用      更新时间:2023-09-26

我有这行

$(document).on('ready',contentLockerShow);

当页面加载时,这行将弹出一个名为contentLockerWrapper的div,我只想将弹出延迟20秒,所以我更改了

$(document).on('ready',contentLockerShow);

带有

setTimeout(contentLockerShow,20000);

但是contentLockerBackground在包装器之前弹出,屏幕在弹出窗口出现之前被锁定。

这是的功能

function contentLockerShow(){
        contentLockerBackground.animate({'opacity':'.6'}, 500);
        contentLockerWrapper.fadeIn(500);    
        if(contentLockerCompleted == false){
            contentLockerCompleted = true;
            console.log(contentLockerCompleted);    
        }

我一开始误解了你的问题。在jQuery中,可以告诉一个动画在另一个动画完成之前不要开始。现在,后台和包装器都需要半秒钟的时间才能同时加载。试试这个:

function contentLockerShow(){
        contentLockerWrapper.fadeIn(500, function()
            contentLockerBackground.animate({'opacity':'.6'}, 500);
        );    
        if(contentLockerCompleted == false){
            contentLockerCompleted = true;
            console.log(contentLockerCompleted);    
        }

类似地,如果你想让窗口等待锁定,直到后台加载完成,你可以像这样进一步修改:

function contentLockerShow(){
        contentLockerWrapper.fadeIn(500, function()
            contentLockerBackground.animate({'opacity':'.6'}, 500, function() 
                 if(contentLockerCompleted == false){
                      contentLockerCompleted = true;
                      console.log(contentLockerCompleted);    
                 }
            );
        );