设置屏幕保护程序的超时

时间:2014-07-07 19:10:14

标签: javascript settimeout screensaver clearinterval

我需要一个设置超时,它会在非活动时间后设置超时,然后启动一个屏幕保护程序,播放幻灯片的轮播,当用户点击屏幕时,它会在启动轮播时重新启动进程在幻灯片开头。以下是我的代码,非常感谢任何帮助!

<script>
    /* The redirect to autoplay page function */
    var screenSaverDelay;
    var screenSaver;
    var destroySlider;

    $(document).ready(function(){

     setInterval(function(){window.focus();},1000);
     $('.bxslider').bxSlider();

    //var screenSaverDelay;

     $('body').click(function(event) {
       killScreenSaver();
     });

    function startScreenSaver() {
        clearTimeout(screenSaver);
        screenSaver=setTimeout(startScreenSaver,1000);
        $('#screensaver').show();
        $('.bxslider').bxSlider();
    }//end

   function killScreenSaver() {
      $('#screensaver').hide();
      $('.bxslider').destroySlider();
      clearTimeout(screenSaver);
      screenSaver=setTimeout(killScreenSaver,300000);
    }//end

    $(document).ready(function(){
    setInterval(function(){window.focus();},1000);
    });
    startScreenSaver();
    });

</script>

2 个答案:

答案 0 :(得分:0)

如果要在设定的时间后启动屏幕保护程序,则不应使用setInterval,应使用setTimeout。您还需要将某些内容绑定到文档,以便在用户单击任何位置时重置超时。

将您的doc.ready更改为以下内容:

$(document).ready(function() {
  var screenSaverTime = 60000, // 1 minute
      myTimer = setTimeout(function() { startScreenSaver(); }, screenSaverTime);      

  $(document).bind(click, function() {
    clearTimeout(myTimer);
    myTimer = setTimeout(function() { startScreenSaver(); }, screenSaverTime);
  });
});

如果您想要侦听许多不同的活动并重置其中任何一个的计时器,请执行以下操作:

var resetTimer;

$(document).ready(function() {
  var screenSaverTime = 60000, // 1 minute
      myTimer = setTimeout(function() { startScreenSaver(); }, screenSaverTime);

  resetTimer = function() {
    clearTimeout(myTimer);
    myTimer = setTimeout(function() { startScreenSaver(); }, screenSaverTime);
  };

  $(document).bind(click, function() {
    resetTimer();
  }).mousemove(function() {
    resetTimer();
  });
});

然后您可以从脚本中的任何其他位置调用resetTimer(只要您的文档已经加载)。

答案 1 :(得分:0)

谢谢大家,这似乎工作正常,干杯

<script>
    /* The redirect to autoplay page function */
    var screenSaverDelay;
    var screenSaver;

    $(document).ready(function(){
        setInterval(function(){window.focus();},1000);
        $('body').click(function(event) {
            killScreenSaver();
        });
        startScreenSaver();
    });

    function startScreenSaver() {
        clearTimeout(screenSaver);
        $('#screensaver').show();
        screenSaver = $('.bxslider').bxSlider();
    }//end

    function killScreenSaver() {
       $('#screensaver').hide();
       screenSaver.destroySlider();
       clearTimeout(screenSaverDelay);
       screenSaverDelay=setTimeout(startScreenSaver,300000);
    }//end


    </script>