为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用随机计时功能显示/隐藏 div

如何使用随机计时功能显示/隐藏 div

泛舟湖上清波郎朗 2023-12-14 16:37:37
我想制作一个小故障动画视频。我用了两个视频来解决这个问题。一个完全出问题了,我只想让她通过具有一定时间范围的随机计时功能随机出现或消失。如何完成或重新编码这一小段 Javascript?我只需要以随机的隐藏时间进行显示/隐藏...显示最多 5 秒,隐藏在 20 秒到 120 秒之间请看一下代码:setTimeout(function(){        document.getElementById('Video1').style.display = 'none';    }, 3000); // 10000ms = 10 seconds<html><head></head><body><div class="Video1" id="Video1" name="Video1">Video1</div><div class="Video2" id="Video2" name="Video2">Video2</div></body></html>
查看完整描述

2 回答

?
慕标琳琳

TA贡献1830条经验 获得超9个赞

递归将调用 setTimeout 并切换 div 的外观。


Video1 将每隔 5 秒播放一次。一旦关闭,它将在 20-120 [s] 间隔内保持这种状态,并且这种情况将无限期地重复。


function callTimeout(isOpen, time) {

  setTimeout(function() {

    if (isOpen) {

      document.getElementById('Video1').style.display = 'none';

      time = (Math.floor(Math.random() * 120) + 20) * 1000;

    } else {

      document.getElementById('Video1').style.display = '';

      time = Math.floor(Math.random() * 6) * 1000;

    }

    isOpen = !isOpen;

    callTimeout(isOpen, time);

  }, time);

}


callTimeout(true, Math.floor(Math.random() * 6) * 1000)

<html>


  <head></head>


  <body>

    <div class="Video1" id="Video1" name="Video1">Video1</div>

    <div class="Video2" id="Video2" name="Video2">Video2</div>

  </body>


</html>


查看完整回答
反对 回复 2023-12-14
?
12345678_0001

TA贡献1802条经验 获得超5个赞

非常好,非常感谢尤金。

我修改了参数以使其更加动态。它太慢了,无法产生故障/无故障效果。


        function callTimeout(isOpen, time) {

  setTimeout(function() {

    if (isOpen) {

      document.getElementById('Video1').style.display = 'none';

      time = (Math.floor(Math.random() * 4) + 1) * 1000;

    } else {

      document.getElementById('Video1').style.display = '';

      time = Math.floor(Math.random() * 2) * 1000;

    }

    isOpen = !isOpen;

    callTimeout(isOpen, time);

  }, time);

}


callTimeout(true, Math.floor(Math.random() * 3) * 1000)

<div class="Video1" id="Video1" name="Video1">Video1</div>

<div class="Video2" id="Video2" name="Video2">Video2</div>


查看完整回答
反对 回复 2023-12-14
  • 2 回答
  • 0 关注
  • 54 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信