-3

这可以使用 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();
        }
      });
    });
4

4 回答 4

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 返回小时。您可以从这里继续 :)

setInterval()更新:如果这是您需要的,则每 5 秒检查一次日期:

<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>
于 2017-01-07T16:45:19.570 回答
0

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

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

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

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

于 2017-01-07T16:40:50.973 回答
0

如果日期小时分钟星期五 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/

虽然不确定是否值得推荐..

于 2017-01-07T16:50:10.433 回答
0

希望能帮助到你。

 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);
于 2017-01-07T16:50:28.193 回答