您将使用窗口的
focus和
blur事件:
var interval_id;$(window).focus(function() { if (!interval_id) interval_id = setInterval(hard_work, 1000);});$(window).blur(function() { clearInterval(interval_id); interval_id = 0;});要回答“ Double Fire”的评论问题并保持在jQuery易用性之内:
$(window).on("blur focus", function(e) { var prevType = $(this).data("prevType"); if (prevType != e.type) { // reduce double fire issues switch (e.type) { case "blur": // do work break; case "focus": // do work break; } } $(this).data("prevType", e.type);})


