-3

これは setInterval() を使用して可能ですか?

以下の例では、アラートは 5 秒ごとです...

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

間隔関数内でサファリ プッシュ通知を実行しようとしています。ベスト プラクティスではないかもしれませんが、現時点では回避策です。

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

0

免責事項:この回答は、必要なアルゴリズムのみを説明しています。

まず、Web サイトのブラウザ ウィンドウが金曜日まで開いていると仮定する必要があります。そうでない場合、これは役に立たず、おそらく Web サイトでこれを行いたくないでしょう。

それでも先に進みたい場合は、次の金曜日の午後 4 時を見つける新しい Date オブジェクトを作成する必要があります。タイムゾーンを考慮する必要があります。GMT、サーバーのタイムゾーン、または固定のタイムゾーン、またはユーザーのタイムゾーンが必要ですか (そして、それを知っていますか?) 次に、そのタイムスタンプと現在の差を計算する必要があります。それをミリ秒に変換し、間隔を設定します。

Get date of specific day of the week 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 に答える