Run setTimeout only when tab is active

前端 未结 6 1938
庸人自扰
庸人自扰 2020-11-29 20:57

Is there a way to stop setTimeout(\"myfunction()\",10000); from counting up when the page isn\'t active. For instance,

  1. A user arrives at a \"some
6条回答
  •  粉色の甜心
    2020-11-29 21:43

    You can do something like:

    $([window, document]).blur(function() {
      // Clear timeout here
    }).focus(function() {
      // start timeout back up here
    });
    

    Window is for IE, document is for the rest of the browser world.

提交回复
热议问题