javascript - 如何在 Bootstrap 模式上设置本地存储?

标签 javascript jquery twitter-bootstrap local-storage

“modal-2”id 打开一个调查模式。

我想要的只是这个特定的模式,在有人点击关闭按钮后每 24 小时重新出现一次。

$(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 并在每次需要决定是否显示模式时检查它。示例代码:

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
}

所以这是您案例中的完整代码:

$(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);
    }
});

关于javascript - 如何在 Bootstrap 模式上设置本地存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36749533/

相关文章:

javascript - Firefox 源少 iframe bug

javascript - 如何用纯 JavaScript 编写以下 JQuery 函数?

twitter-bootstrap - Bootstrap垂直网格

jquery - 像素大小减小时如何堆叠下拉菜单位置

javascript - Bootstrap 选择器的链式下拉菜单不起作用

javascript - 苹果 360 示例

javascript - 在$ .ajax()调用之一中转义$ (“html”).ajaxError()

javascript - 如何使用 Twilio SMS/语音 api 实现一次性验证 (OTP)

javascript - 有没有办法在仅使用 JS/HTML 的 AIR 应用程序中拥有 Webkit 浏览器?

jQuery悬停效果问题