Javascript 在显示div之前设置超时

Javascript 在显示div之前设置超时,javascript,jquery,jquery-ui,javascript-events,Javascript,Jquery,Jquery Ui,Javascript Events,我有个问题,我想在5秒后显示一个div,我尝试了: if(obj.google_analytics.is_casino_game){ setTimeout(function() { }, 500); } document.getElementById('addsense-pub').setAttribute('class','display-block'); 但不是工作。你能帮我吗?Thx提前我不确定我是否得到它,但您至少必须将要执行的代码放入超时内 if(obj.google_analy

我有个问题,我想在5秒后显示一个div,我尝试了:

if(obj.google_analytics.is_casino_game){
  setTimeout(function() { }, 500);
}
document.getElementById('addsense-pub').setAttribute('class','display-block');

但不是工作。你能帮我吗?Thx提前

我不确定我是否得到它,但您至少必须将要执行的代码放入超时内

if(obj.google_analytics.is_casino_game){
    setTimeout(function() {
        document.getElementById('addsense-pub').setAttribute('class','display-block');
    }, 5000);
}

请注意,以这种方式隐藏和显示来自adSense的广告通常被视为破坏TOS

您可以使用
delay

if (obj.google_analytics.is_casino_game) {
    $('#addsense-pub').delay(5000).show();
    // Will add delay of 5 seconds before showing the element
}
文件:

使用
setTimeout

if (obj.google_analytics.is_casino_game) {
    setTimeout(function () {
        $('#addsense-pub').show();
    }, 5000);
}

在setTimeout函数()中编写代码

 setTimeout(function() { 
   $('#addsense-pub').show();

}, 5000);