【问题标题】:How to run a Javascript alert every Friday at 16:00如何在每周五 16:00 运行 Javascript 警报
【发布时间】:2017-01-07 16:32:58
【问题描述】:

这可以使用 setInterval() 吗?

在下面的示例中,警报是每 5 秒...

<script type="text/javascript">
  setInterval(function() {
    // alert("Message to alert every 5 seconds");
}, 5000);
</script>

我正在尝试在间隔函数中运行 safari 推送通知,也许这不是最佳做法,但目前是一种解决方法

function Notifier() {}

    // Returns "true" if this browser supports notifications.
    Notifier.prototype.HasSupport = function() {
      if (window.webkitNotifications) {
        return true;
      } else {
        return false;
      }
    }

    // Request permission for this page to send notifications. If allowed,
    // calls function "cb" with true.
    Notifier.prototype.RequestPermission = function(cb) {
      window.webkitNotifications.requestPermission(function() {
        if (cb) { cb(window.webkitNotifications.checkPermission() == 0); }
      });
    }

    // Popup a notification with icon, title, and body. Returns false if
    // permission was not granted.
    Notifier.prototype.Notify = function(icon, title, body) {
      if (window.webkitNotifications.checkPermission() == 0) {
        var popup = window.webkitNotifications.createNotification(
        icon, title, body);
        popup.show();

        return true;
      }

      return false;
    }

    $(function() {
      var notifier = new Notifier();
      if (!notifier.HasSupport()) {
        $("#error").show();
        return;
      }

      $("#request-permission").click(function() {
        $("#error").hide();
        notifier.RequestPermission();
      });

      $(function() {
        if (!notifier.Notify("#", "MESSAGE")) {
          $("#error").text('Permission denied. Click "Request Permission" to give this domain access to send notifications to your desktop.');
          $("#error").show();
        } else {
          $("#error").hide();
        }
      });
    });

【问题讨论】:

  • 这里更像是一道数学题 :)) 每周五 16:00 之间有多少毫秒
  • 5 秒循环与星期五下午 4 点有什么关系?
  • @j08691 我需要在每周五 16:00 运行此警报,这对您来说有意义吗?
  • 这只有在网站打开了很长时间才有意义。
  • 您需要日期计算,这里 setInterval() 无济于事...

标签: javascript setinterval safari-push-notifications


【解决方案1】:

你可以使用类似的东西:

<script type="text/javascript">
    var date = new Date();
    console.log(date.getDay());
    console.log(date.getHours());
    if(date.getDay() === 6 && date.getHours() === 17) {
        console.log("HELLO WORLD!");
    }
</script>

getDay() 返回从 1 到 7 的星期几(星期五是 5),getHours() 返回从 1 到 24 的小时。您可以从这里继续 :)

更新:如果这是您需要的,这将每 5 秒使用setInterval() 检查日期:

<script type="text/javascript">

function checkDate() {
    var date = new Date();
    console.log(date.getDay());
    console.log(date.getHours());
    if(date.getDay() === 6 && date.getHours() === 17) {
        console.log("HELLO WORLD!");
    }
}

var dateLoop = setInterval(function() {
    checkDate();
},5000);
</script>

【讨论】:

  • 然后循环运行?
  • 是的,无论你需要做什么,你都可以将它放在一个在间隔循环中运行的函数中,或者与其他有效的事件一起运行。
  • 一直运行它听起来非常昂贵。好吧,也许当你每分钟运行一次它就可以了。与下周五的实际计算相比,这肯定是更多的蛮力方法。
【解决方案2】:

免责声明:此答案仅描述所需的算法。

首先,您需要假设网站的浏览器窗口一直打开到星期五。如果没有,这是没用的,您可能不想在网站上这样做。

如果您仍想继续,您需要创建一个新的 Date 对象,您可以在其中找到下周五下午 4 点。您必须考虑时区。你想要格林威治标准时间,还是服务器的时区,或者一个固定的时区,或者用户的时区(你甚至知道吗?)然后你必须计算那个时间戳和现在之间的差异。将其转换为毫秒并设置间隔。

Get date of specific day of the week in JavaScript 中有几个关于如何获取某个工作日的未来日期的好答案。从那里,您只需要进行一些基本的计算。

【讨论】:

  • 我已经编辑了问题以明确我的意图
  • 我在这里描述的方法不会随着你@NinjaShawarma的澄清而改变。
【解决方案3】:

希望对你有帮助。

 var dayOfWeek = 5; // friday
 var date = new Date();
 date.setDate(date.getDate() + (dayOfWeek + 7 - date.getDay()) % 7);
 date.setHours(16,0,0,0)

 var dif = date.getTime() - new Date().getTime();
 console.log(dif);

 setTimeout(function () {
    // send alert or push notification

 }, dif);

【讨论】:

  • 无论 setHours() 是什么,它都会继续发送 Timeout 函数。只是为了尝试该功能,我将 dayOfWeek 更改为 6,即 Sat。见jsfiddle.net/15p21uxv让我知道你的想法。谢谢!
【解决方案4】:

如果 dayhoursminutesseconds 匹配 ,您可以检查每一秒周五 16:00:00

window.onload = function() {
  setInterval(function() {
    var date = new Date();
    if (date.getDay()==5 && date.getHours()==16 && date.getMinutes()==0 && date.getSeconds()==0) {
      alert("4 O'CLOCK!");
    }
  },1000);
};
jsfiddle:https://jsfiddle.net/dwdek28r/1/

不确定是否值得推荐..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-09
    • 2015-09-24
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 2016-05-28
    • 2021-04-18
    相关资源
    最近更新 更多