简体   繁体   English

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

[英]How to set Cookie for whole login session using Onclick Jquery event

Here i like explain my problem clearly,在这里我想清楚地解释我的问题,

Actually am having a notification icon in my appication, which is used to show count of rows in table called Delay.实际上,我的应用程序中有一个通知图标,用于显示名为 Delay 的表中的行数。 like below像下面

图片!

so now what i need is, when i used click the notification icon it must set cookie and then after 5 seconds count will automatically fadeout.所以现在我需要的是,当我使用单击通知图标时,它必须设置 cookie,然后在 5 秒后计数将自动淡出。

then if cookie is equal to the cookie which i set, it must hide the div #not-count for 1 hour.然后,如果 cookie 等于我设置的 cookie,它必须隐藏 div #not-count 1 小时。

this my code for fadeout after 5 seconds without cookie这是我在没有 cookie 的情况下 5 秒后淡出的代码

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

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

    }) 

Help me to sort out this.帮我解决这个问题。

Here is the fiddle .这是小提琴

Here is the markup这是标记

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

Here is the code (you may want to wrap it in the proper lexical scope)这是代码(您可能希望将其包装在适当的词法范围内)

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;
}

Create/Read cookie functions taken from this SO thread solution by @Mandeep Janjua从@Mandeep Janjua 的这个SO 线程解决方案中获取的创建/读取 cookie 函数

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

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