显示div 15秒然后显示另一个div

时间:2012-08-29 07:28:09

标签: javascript delay setinterval

我有一个游戏网站,我想在游戏前放一个广告。那么有人可以告诉我如何显示广告div 15秒,在下面说。 “游戏将在[剩余时间]秒开始”。然后一个时间到了,那么它将显示持有游戏的div。请在javascript中。

谢谢

5 个答案:

答案 0 :(得分:3)

我建议您查看setTimeout。它允许你在一段时间后做某事。如果您需要更多帮助,请告诉我们。

编辑:对不起,我误解了你的问题。您应该使用的更合适的方法是setInterval,所以在15秒内每秒钟都可以显示时间。斯佩兰斯基的回答是正确的。

答案 1 :(得分:2)

演示:http://jsfiddle.net/myDTK/1

var secondsLeft = 15;
var delay = 1000; // 1 second

var interval = setInterval(function () {
  if (secondsLeft > 0) {
    document.getElementById('timer').innerHTML = 'The game will start in ' + secondsLeft + ' seconds...';
    secondsLeft -= 1;
  } else {
    document.getElementById('timer').style.display = 'none';
    clearInterval(interval);
    document.getElementById('game').style.display = 'block';
  }
}, delay);​

答案 2 :(得分:1)

    function doSome() {
       //do some
    }

    setTimeout(function(){
       doSome();
    }, 15000);

答案 3 :(得分:0)

查看setTimeout函数

例如

setTimeout(function(){
  //code to hide the div and how game div
}, 15000);

答案 4 :(得分:0)

这是使用jQuery的live example

基本上只需要创建一个添加div的函数,在需要时调用该函数,然后在该函数中执行setTimeout(removeFunction, 15000)以在15秒后调用remove函数。

为了进行倒数计时器,您可以每秒调用一次函数updateTimer(同样,setTimeout(updateTimer, 1000))并让该函数只搜索计时器并递减计数器。

相关问题