简单的 JavaScript 警报在一定时间内不能多次触发?
只是想知道是否有一种简单的方法可以让警报在特定时间段内不能多次触发。我所使用的
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(5)
您可能希望以不同的方式实现每 4 小时调用一次以上的效果。
You may want to implement the effect of calling it more then once every 4 hours differently.
跟踪您何时发出警报并将其保存在客户端计算机上:
并使用
doAlert
而不是alert
,后者现在委托警报。Keep track of when you alert and save that on the client's computer:
And use
doAlert
instead ofalert
, which now delegates alerts.一种方法是将第一个警报发生时的时间戳存储在 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
你必须创建一个函数来测试这类事情。类似这样的事情是可行的:
你可以随心所欲地调用alertLimited(test),但实际上它只会至少每4小时调用一次alert()。
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.
此处的工作示例: http://jsfiddle.net/jfriend00/xUWf5/
Working example here: http://jsfiddle.net/jfriend00/xUWf5/