javascriptjqueryscreensaverscrypt

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


I have a screensaver script and it shows a screensaver after some time (e.g. 10 sec). How to make to stop screensaver after some time when it's started (e.g. after 5 sec) and then repeat again the same every (e.g. 10sec) but to calculate time from when it stop.

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);
  }
}

Solution

  • Try calling stop function within start function like this

    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);
    }