简体   繁体   English

如何在PHP中打开的选项卡上的活动页面中检查此页面

[英]How to check if this page in active page on opened tabs in php

In instagram while you opened and looking someone story it has a timeout until goes to next story, but when you changing browser tab it stops and when you back it goes from where you left page. 在instagram中,当您打开并查看某人的故事时,它会一直超时直到下一个故事,但是当您更改浏览器选项卡时,它会停止,而当您返回时,它会从离开页面的位置开始。 sorry for my bad English I hope you have got it. 对不起,我的英语不好,我希望你明白了。

So how can I do that in php, I want to stop a countdown when user leaves the tab. 所以我该如何在php中做到这一点,我想在用户离开标签页时停止倒计时。

You can use the following function to get when the user leaves 您可以使用以下功能获取用户离开的时间

$(window).blur(function(e) {
    // What to do when user leaves
});

And then this other to get when the user returns 然后当用户返回时获得另一个

$(window).focus(function(e) {
    // What to do when user comes back
});

Use it on ready because it will automatically trigger the focus function, and it can throw errors if the DOM isn't loaded yet. 使用它的ready ,因为它会自动触发focus功能,如果DOM尚未加载它可以引发错误。 Also remember to prepare it to don't do anything you do on focus at first time, except it isn't a problem. 还要记住,要做好准备,使其在第一时间不做任何事情,除非这不是问题。

Here is an example how to keep your animations in sync when browser tab is changed . 这是一个示例,如何在更改浏览器选项卡时使动画保持同步

You could use the same approach to pause/resume your story timeout when current browser tab is in/active using HTML 5 visibility API like so: 当当前浏览器选项卡处于启用状态或处于活动状态时,您可以使用相同的方法使用HTML 5可见性API来暂停/恢复故事超时,如下所示:

var vis = (function(){
var stateKey, 
    eventKey, 
    keys = {
            hidden: "visibilitychange",
            webkitHidden: "webkitvisibilitychange",
            mozHidden: "mozvisibilitychange",
            msHidden: "msvisibilitychange"
};
for (stateKey in keys) {
    if (stateKey in document) {
        eventKey = keys[stateKey];
        break;
    }
}
return function(c) {
    if (c) document.addEventListener(eventKey, c);
    return !document[stateKey];
}})();

Live preview on codepen: https://codepen.io/jonathan/pen/sxgJl 在Codepen上进行实时预览: https ://codepen.io/jonathan/pen/sxgJl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM