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>

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>
添加回答
舉報