检查过去一天是否点击了链接

时间:2016-01-19 03:12:39

标签: jquery html hyperlink click show-hide

我正在尝试使用本地html和Jquery为自己创建一个简单的个人主页。我添加了一个链接到Bing Rewards。

<a id="bing" href="https://www.bing.com/rewards/dashboard">Bing Rewards</a>

但是,我只想每天去这个网站一次。当我点击它时,我希望它隐藏到第二天。

$('#bing').click(function(){
   $(this).hide();
}

当我关闭并重新打开页面时,我如何才能让它知道我上次访问链接的时间,并在此基础上显示链接? 我对这种东西很陌生,所以答案越简单越好。谢谢!

3 个答案:

答案 0 :(得分:1)

在此方法中,最后一次点击的日期(从午夜开始)存储在本地存储空间中(demo

# get all unique tags
all_tags = list(set([item for sublist in df.tags.tolist() for item in sublist]))

# get a count of each tag 
tag_views = {tag: df[df.tags.map(lambda x: tag in x)].views.sum() for tag in all_tags}

答案 1 :(得分:1)

您可以将点击时间存储在Cookie中。使用像js-cookie这样的库会很容易。

$('#bing').click(function() {
    $(this).hide();

    // set cookies to expire in 14 days
    Cookies.set('bing-click-time', new Date(), {
        expires : 14
    });
});
var clickTime = Cookies.get('bing-click-time');
if (clickTime != undefined) {
    var cTime = new Date(clickTime);
    var diff = new Date().getTime() - cTime.getTime();
    console.log(clickTime, cTime, cTime.getTime(), diff / (1000 * 60))
    if (diff / (1000 * 60) < 3) {// eg: less than 3 min
        $('#bing').hide();
    }
}

答案 2 :(得分:1)

以下将隐藏或显示页面加载按钮并在localStorage中存储时间。

如果页面长时间保持打开状态,您可能需要运行间隔计时器来检查日期差异

var lastBing = localStorage.getItem('last_bing'),
    now = new Date(),
    showButton = true;       
if (lastBing) {
    lastBing = new Date(+lastBing);
    showButton = lastBing.setDate(lastBing.getDate() + 1) < now;
}

$('#bing').click(function() {
    localStorage.setItem('last_bing', +new Date());
    $(this).hide();
}).toggle(showButton);
相关问题