简单的 JavaScript 警报在一定时间内不能多次触发?

发布于 2024-11-29 04:54:23 字数 115 浏览 0 评论 0原文

只是想知道是否有一种简单的方法可以让警报在特定时间段内不能多次触发。我所使用的

alert(test)

只是不希望用户在 4 小时内多次看到此警报。

Just wondering if there is a simple way to have an alert that can't fire more then once in a certain time period. All I'm using is

alert(test)

I just don't want users to see this alert more then once in a 4 hour period.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

单身狗的梦 2024-12-06 04:54:23
(function() {
  var _alert = window.alert,
      queue = [];
  window.alert = function(msg) { 
      queue.push(msg);
  }
  setInterval(function() {
    if (queue.length !== 0) {
      _alert(queue.pop());
    }
  }, 1000 * 60 * 4);
})();

您可能希望以不同的方式实现每 4 小时调用一次以上的效果。

(function() {
  var _alert = window.alert,
      queue = [];
  window.alert = function(msg) { 
      queue.push(msg);
  }
  setInterval(function() {
    if (queue.length !== 0) {
      _alert(queue.pop());
    }
  }, 1000 * 60 * 4);
})();

You may want to implement the effect of calling it more then once every 4 hours differently.

且行且努力 2024-12-06 04:54:23

跟踪您何时发出警报并将其保存在客户端计算机上:

function doAlert(text) {
    if(!localStorage['last_alert'] || new Date - localStorage['last_alert'] > 4 * 60 * 60 * 1000) {
        localStorage['last_alert'] = +new Date;
        alert(text);
    }
}

并使用 doAlert 而不是 alert,后者现在委托警报。

Keep track of when you alert and save that on the client's computer:

function doAlert(text) {
    if(!localStorage['last_alert'] || new Date - localStorage['last_alert'] > 4 * 60 * 60 * 1000) {
        localStorage['last_alert'] = +new Date;
        alert(text);
    }
}

And use doAlert instead of alert, which now delegates alerts.

余生再见 2024-12-06 04:54:23

一种方法是将第一个警报发生时的时间戳存储在 cookie 中,然后下次当您到达需要再次调用警报的点时加载 cookie(如果存在,如果不存在)不提醒)并检查自上次以来时间是否大于 4 小时,如果是则再次提醒,否则不要

One way to do this would be to store the timestamp of when the first alert happened in a cookie, and then next time when you get to the point where you need to call the alert again load the cookie (if its there, if it isn't alert) and check if the time is greater then 4 hours since the last time and if so alert again otherwise don't

一人独醉 2024-12-06 04:54:23

你必须创建一个函数来测试这类事情。类似这样的事情是可行的:

你可以随心所欲地调用alertLimited(test),但实际上它只会至少每4小时调用一次alert()。

var alertLimited = function () {
    var lastAlertTime = 0;
    return function alertLimited(input) {
        var current = new Date();
        current.setHours(current.getHours() - 4);
        if ((current - lastAlertTime) > 0) {
            lastAlertTime = new Date();
            alert(input);
        }
    };
}();

You'd have to make a function to test that kind of thing. Something along the lines of this would work:

You could call alertLimited(test) all you want, but it would only actually call alert() a minimum of once every 4 hours.

var alertLimited = function () {
    var lastAlertTime = 0;
    return function alertLimited(input) {
        var current = new Date();
        current.setHours(current.getHours() - 4);
        if ((current - lastAlertTime) > 0) {
            lastAlertTime = new Date();
            alert(input);
        }
    };
}();
三五鸿雁 2024-12-06 04:54:23
var lastShown = new Date(1900, 1, 1);  // initialize

function showMsg(msg) {
    var minTime = 4 * 60 * 60 * 1000;  // 4 hrs seconds (set this to however long you want)
    var now = Date.now();
    if (now - lastShown < minTime) return;  // just return if not enough time has elapsed
    lastShown = now;
    alert(msg);
}

此处的工作示例: http://jsfiddle.net/jfriend00/xUWf5/

var lastShown = new Date(1900, 1, 1);  // initialize

function showMsg(msg) {
    var minTime = 4 * 60 * 60 * 1000;  // 4 hrs seconds (set this to however long you want)
    var now = Date.now();
    if (now - lastShown < minTime) return;  // just return if not enough time has elapsed
    lastShown = now;
    alert(msg);
}

Working example here: http://jsfiddle.net/jfriend00/xUWf5/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文