如何在引导程序模式下设置本地存储? [英] How do I set local storage on a bootstrap modal?

查看:40
本文介绍了如何在引导程序模式下设置本地存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

"modal-2" ID打开调查的模式.

The "modal-2" id opens a modal for a survey.

我只想针对这种特定模式,在有人单击关闭按钮后每24小时重新出现一次.

All I want is for this particular modal, to re-appear once every 24 hours after someone clicks the close button.

$(document).ready(function(){
    var modals = ['#events'];
    if (window.location.hash && ~modals.indexOf(window.location.hash)) {
        $(window.location.hash).modal();
    }
    $("#modal-2").modal('show');
    $(".modal:not(.noclose)").on("click","a",function(){
        $(this).closest(".modal").modal("hide");
    });
});

推荐答案

您可以将当前时间戳记Date.now()设置为localStorage,并在每次需要决定是否显示模式时进行检查.示例代码:

You can set current timestamp Date.now() to the localStorage and check it every time you need to decide whether to show the modal or not. Example code:

var twentyFourHoursInMs = 24 * 60 * 60 * 1000;
var lastTimestamp = Number(localStorage.getItem("last-showed-at"));
var currentTimestamp = Date.now();
if ((currentTimestamp - lastTimestamp) >= twentyFourHoursInMs) {
    localStorage.setItem("last-showed-at", currentTimestamp);
    $("#your-modal-id").modal("show");
    // Display modal once again
}

这是您所用的完整代码:

So this is the full code in your case:

$(document).ready(function(){
    var modals = ['#events'];
    if (window.location.hash && ~modals.indexOf(window.location.hash)) {
        $(window.location.hash).modal();
    }

    $(".modal:not(.noclose)").on("click","a",function(){
        $(this).closest(".modal").modal("hide");
    });

    var currentTimestamp = Date.now();

    $("#cul8a").on("hidden.bs.modal", function () {
        localStorage.setItem("last-showed-at", currentTimestamp);
    });

    // Check for modal eligibility

    var twentyFourHoursInMs = 24 * 60 * 60 * 1000;
    var lastTimestamp = Number(localStorage.getItem("last-showed-at"));

    if ((currentTimestamp - lastTimestamp) >= twentyFourHoursInMs) {
        setTimeout(function() {
            localStorage.setItem("last-showed-at", currentTimestamp);
            $("#cul8a").modal("show");
        }, 4000);
    }
});

这篇关于如何在引导程序模式下设置本地存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆