-3

Is this possible using setInterval()?

In the below example, the alert is every 5 seconds...

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

I'm trying to run safari push notification inside the interval function, maybe it isn't the best practice, but it's a workaround for now

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();
        }
      });
    });
NinjaShawarma
  • 69
  • 1
  • 1
  • 9

4 Answers4

2

You can use something like:

<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() returns the day of the week from 1 to 7 (friday would be a 5) and getHours() returns the hour from 1 to 24. You can continue from here :)

Update: this checks the date every 5 seconds with setInterval() if that's what you need:

<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>
MarioZ
  • 981
  • 7
  • 16
  • And then run it in a loop? – simbabque Jan 07 '17 at 16:45
  • Yes, whatever you need to do you can put this inside a function that runs in an interval loop, or with other event that works. – MarioZ Jan 07 '17 at 16:48
  • Running it all the time sounds very expensive. Well, maybe when you run it once a minute it would be ok. It certainly is more of the brute force approach compared to actually calculating next Friday. – simbabque Jan 07 '17 at 16:49
0

Disclaimer: this answer only describes the needed algorithm.

First, you need to assume the browser window with the website stays open until Friday. If not, this is useless and you probably don't want to do this on a website.

If you still want to go ahead, you need to create a new Date object where you find the next Friday at 4pm. You would have to take into account the timezone. Do you want GMT, or the server's timezone, or a fixed one, or the user's timezone (and do you even know that?) Then you have to calculate the difference between that timestamp and now. Convert that to milliseconds and set your interval.

There are several good answers on how to obtain a future date with a certain weekday in Get date of specific day of the week in JavaScript. From there, you only need to do some basic calculations.

Community
  • 1
  • 1
simbabque
  • 53,749
  • 8
  • 73
  • 136
0

You could just check every second if the day, hours, minutes and seconds match Friday 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/

Not sure if it's recommendable though..

myfunkyside
  • 3,890
  • 1
  • 17
  • 32
0

Hope it helps.

 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);
Engin
  • 755
  • 7
  • 20
  • This keeps sending the Timeout function no matter what the setHours() is. Just for the sake of trying the function, I changed the dayOfWeek to 6 which is Sat. See https://jsfiddle.net/15p21uxv/ Let me know what you think. Thanks! – NinjaShawarma Jan 07 '17 at 18:48