Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.JS&;Socket.io-检查是否选中该选项卡_Javascript_Node.js - Fatal编程技术网

Javascript Node.JS&;Socket.io-检查是否选中该选项卡

Javascript Node.JS&;Socket.io-检查是否选中该选项卡,javascript,node.js,Javascript,Node.js,对于我的聊天,我希望有通知。这些通知的作用类似于Gitter的通知,它更改html标题以显示您有消息。我在谷歌上搜索了如何实现这一点,但所有的答案只有在标签更改时才起作用。比如说, socket.on('chat message', function (msg) { // Append the message appendMessage(msg); // Check if the window is focused if (window.onfocus) {

对于我的聊天,我希望有通知。这些通知的作用类似于Gitter的通知,它更改html标题以显示您有消息。我在谷歌上搜索了如何实现这一点,但所有的答案只有在标签更改时才起作用。比如说,

socket.on('chat message', function (msg) {
    // Append the message
    appendMessage(msg);

    // Check if the window is focused
    if (window.onfocus) {

        // If it is, there's no need to show there's a new message
        document.title = "ChatProject";

    }else{

        // If the user is on another tab, show there's a new message
        document.title = "[!] ChatProject";
    }
});

使用上面的代码,无论您是否在选项卡上,它都会显示通知。如何使其仅在有新消息时显示?

window.onfocus是一个事件。不是一个州

通过向事件添加getter和setter,可以获得所描述的行为

///////////////////////
//Setting OnFocusSate//
///////////////////////
var isFocused = true;


function onFocus(){
    isFocused = true;
};

function onBlur() {
    isFocused = false;
};

window.onfocus = onFocus;
window.onblur = onBlur;

///////////////////////
//Example Event Check//
///////////////////////

socket.on('chat message', function (msg) {
    // Append the message
    appendMessage(msg);

    // Check if the window is focused
    if (isFocused) {

        // If it is, there's no need to show there's a new message
        document.title = "ChatProject";

    }else{

        // If the user is on another tab, show there's a new message
        document.title = "[!] ChatProject";
    }
});
神秘:这是可行的,但是当你点击标签上的后退时,通知不会消失。因此,我们必须将if语句改为

       if (!isFocused) {    
        // If the user is on another tab, show there's a new message
        document.title = "[!] ChatProject";
        }
然后把这个加在下面

window.onfocus = function() {
    document.title = "ChatProject";
}

你能展示一下发射的代码吗?您当前的代码看起来正确。。发出的代码工作正常-
socket.emit('chat message',reply.val());//发出有趣的消息
。。如果您在回调上的
中打开调试器并带有断点,然后更改选项卡,标题是否立即更改,断点是否未被命中?实际上,我刚刚意识到这是错误的代码(我忘记保存更改)。对于原始帖子中的代码,无论您是否在选项卡上,它都会显示通知。好吧,我想我对编辑的代码测试得不够好。很奇怪,我第一次发信息时,没关系。但是当我发送第二条消息(等等)时,它显示我在活动选项卡上有一个通知。我制作了一个.gif文件,以便进行更改