如何让屏保根据时间出现和消失?

How to make screensaver to appear and disappear based on time?

我有一个屏保脚本,它会在一段时间后(例如 10 秒)显示一个屏保。如何在屏幕保护程序启动一段时间后(例如 5 秒后)停止屏幕保护程序,然后每隔一段时间(例如 10 秒)再次重复相同的操作,但要从停止时计算时间。

var screensaver_active = false;

function show_screensaver() {
  $('#screensaver').fadeIn();
  screensaver_active = true;
  screensaver_animation();
}

function stop_screensaver() {
  $('#screensaver').fadeOut();
  screensaver_active = false;
}

function getRandomColor() {
  var letters = '0123456789ABCDEF'.split('');
  var color = '#';
  for (var i = 0; i < 6; i++) {
    color += letters[Math.round(Math.random() * 15)];
  }
  return color;
}


setInterval(function() {
  show_screensaver();
  stop_screensaver();
}, 10000);

function screensaver_animation() {
  if (screensaver_active) {
    $('#screensaver').animate({
      backgroundColor: getRandomColor()
    }, 400, screensaver_animation);
  }
}

尝试像这样在启动函数中调用停止函数

function show_screensaver() {
$('#screensaver').fadeIn();
screensaver_active = true;
screensaver_animation();
setTimeout(function(){stop_screensaver();},5000);
}
function stop_screensaver() {
  $('#screensaver').fadeOut();
  screensaver_active = false;
setTimeout(function(){show_screensaver();},10000);
}