Javascript 如何使用Onclick Jquery事件为整个登录会话设置Cookie

Javascript 如何使用Onclick Jquery事件为整个登录会话设置Cookie,javascript,php,jquery,cookies,Javascript,Php,Jquery,Cookies,在这里我喜欢清楚地解释我的问题 实际上,我的应用程序中有一个通知图标,用于显示名为Delay的表中的行数。如下 所以现在我需要的是,当我使用单击通知图标时,它必须设置cookie,然后在5秒后计数将自动淡出 然后,如果cookie等于我设置的cookie,它必须将div#not count隐藏1小时 这是我的代码,用于5秒后在没有cookie的情况下淡出 $(document).ready(function($){ $('#notify-comet').on('click',

在这里我喜欢清楚地解释我的问题

实际上,我的应用程序中有一个通知图标,用于显示名为Delay的表中的行数。如下

所以现在我需要的是,当我使用单击通知图标时,它必须设置cookie,然后在5秒后计数将自动淡出

然后,如果cookie等于我设置的cookie,它必须将div
#not count
隐藏1小时

这是我的代码,用于5秒后在没有cookie的情况下淡出

$(document).ready(function($){

        $('#notify-comet').on('click', function(e){
            setTimeout(function() {
                $('#not-count').fadeOut('fast');
            }, 1000); 
        })

    }) 
请帮我整理一下。

这是答案

这是标记

<input type='button' value='btn' id='notify-comet' />
<div id='not-count'>test</div>

测试
以下是代码(您可能希望将其包装在适当的词法范围内)

var x=readCookie('notCount')
if(x)
{
$(“#不计算”).hide();
}
$('notify comet')。在('click',函数(e){
如果(!x)
{
setTimeout(函数(){
//我已将“notCount”cookie设置为1分钟后过期。您可以将其设置为60分钟
createCookie('notCount','true',1);
$(“#不计算”).fadeOut('fast');
}, 5000);       
}
});
函数createCookie(名称、值、分钟){
若有(分钟){
变量日期=新日期();
date.setTime(date.getTime()+(分钟*60*1000));
var expires=“;expires=“+date.togmString();
}
else var expires=“”;
document.cookie=name+“=”+value+expires+“path=/”;
}
函数readCookie(名称){
变量nameEQ=name+“=”;
var ca=document.cookie.split(“;”);
对于(变量i=0;i

创建/读取@Mandeep Janjua从此解决方案中获取的cookie函数

您在这项工作中使用了错误的东西您可以使用推送消息通知来完成这项工作是的,我知道,但我不想这样做。。
var x = readCookie('notCount')

if(x)
{
    $('#not-count').hide();
}

$('#notify-comet').on('click', function(e){
    if(!x)
    {
        setTimeout(function() {
            //I've set the 'notCount' cookie to expire in 1 minute. You can set it to 60 minutes
            createCookie('notCount', 'true', 1);
            $('#not-count').fadeOut('fast');       
        }, 5000);       
    }

});

function createCookie(name,value,minutes) {
    if (minutes) {
        var date = new Date();
        date.setTime(date.getTime()+(minutes*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}