简体   繁体   English

仅显示首次观看者的消息

[英]Display message for first time viewer only

Im trying to display a welcome message for a few seconds then have it fade out and not return (unless user deletes cookies). 我试图显示欢迎消息几秒钟,然后逐渐消失而不返回(除非用户删除cookie)。 I know i can do this 2 ways using either js cookies or localStorage. 我知道我可以使用js cookie或localStorage做到这两种方式。 This is the code i'm using: 这是我正在使用的代码:

$(document).ready(function() {
    if(localStorage.getItem('messageState') != 'shown'){
        $("#message").delay(2000).fadeOut();
        localStorage.setItem('messageState','shown')
    }

    $('#message').fadeOut(); 
    });
});

But it's not fading out. 但这并没有消失。 Any ideas what i'm doing wrong? 任何想法我在做什么错?

Or would i be better using js cookie? 还是我会更好地使用js cookie?

Here's a fiddle: http://jsfiddle.net/Y2D67/1786/ 这是一个小提琴: http : //jsfiddle.net/Y2D67/1786/

Delete your last line, the }); 删除最后一行, }); and the fadeout works. 和淡出效果。

$(document).ready(function() {
    if(localStorage.getItem('messageState') != 'shown'){
        $("#message").delay(2000).fadeOut();
        localStorage.setItem('messageState','shown')
    }


    $('#message').fadeOut(); 
});

Your syntax is wrong, the closing bracket 您的语法有误,右括号

 })

does not belong 不属于

Localstorage would be a fine way to store something like this, I usually go by the rule that localstorage is for data I want to read on the front end and cookies are primarily for reading server-side Localstorage是一种存储类似内容的好方法,我通常遵循以下规则:localstorage用于存储要在前端读取的数据,而cookie主要用于读取服务器端

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

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