2017-01-07 2 views
-3

setInterval()을 사용하여 가능합니까? 아래의 예에서 매주 금요일 16:00에 Javascript 알림을 실행하는 방법

, 경고 내가 어쩌면 가장 좋은 방법이 아닙니다, 간격 함수 내에서 사파리 푸시 알림을 실행하기 위해 노력하고있어

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

... 5 초마다,하지만 그건 지금

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(); 
     } 
     }); 
    }); 
+0

그것은 여기 수학 문제처럼 더 :))하는 시간을 밀리 초 단위로 각 금요일 16시 사이 – NinjaShawarma

+1

무엇 5 초 루프 오후 4 @ 금요일에해야 할 것인가? – j08691

+0

@ j08691 매주 금요일 16시에이 경고가 필요합니다. 이해가 되시나요? – NinjaShawarma

답변

1

당신은 같은 것을 사용할 수 있습니다 : 1 당신은 여기에서 계속 (24)과 getHours() 반환 시간 (5에게 것 금요일) 1 ~ 7

<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() 반환 요일을 :)

업데이트 : 그건 경우이 당신이 필요 날짜를 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> 
+0

그런 다음 루프에서 실행 하시겠습니까? – simbabque

+0

예, 당신이해야 할 일은 간격 루프 또는 다른 작동하는 이벤트에서 실행되는 함수 안에 넣을 수 있습니다. – MarioZ

+0

항상 실행하면 매우 비쌉니다. 어쩌면 1 분에 1 번 실행하면 괜찮을 것입니다. 그것은 분명히 다음 금요일 계산에 비해 무차별 대입 접근법입니다. – simbabque

0

면책 조항에 대한 해결 방법 :이 응답은 필요한 알고리즘을 설명합니다.

먼저 웹 사이트가있는 브라우저 창이 금요일까지 열려 있다고 가정해야합니다. 그렇지 않다면 이것은 쓸모가 없으므로 아마 웹 사이트에서 이것을 원하지 않을 것입니다.

계속 진행하려면 다음 금요일 오후 4시에 새로운 Date 개체를 만들어야합니다. 시간대를 고려해야합니다. GMT 또는 서버의 시간대 또는 고정 된 시간대 또는 사용자의 시간대를 원하십니까? (그리고 당신도 알 수 있습니까?) 그런 다음 해당 시간 소인과 현재 시간의 차이를 계산해야합니다. 이를 밀리 초로 변환하고 간격을 설정하십시오.

Get date of specific day of the week in JavaScript에 특정 요일의 미래 날짜를 얻는 방법에 대한 여러 가지 좋은 답변이 있습니다. 거기에서 몇 가지 기본 계산 만하면됩니다.

+0

의도를 분명히하기 위해 질문을 편집했습니다. – NinjaShawarma

+0

여기서 설명한 방법은 설명과 함께 변경되지 않습니다. @ NinjaShawarma. – simbabque

0

당신은 확인할 수있는 모든 두 번째 경우 , 시간, 경기 금요일 16시 0분 0초

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/

그래도 괜찮은지 잘 모르겠다.

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); 
+0

이것은 setHours()가 무엇이든 상관없이 Timeout 함수를 계속해서 보냅니다. 함수를 사용하기 위해서, 나는 dayOfWeek를 Sat. 6으로 변경했다. https://jsfiddle.net/15p21uxv/ 귀하의 의견을 알려주십시오. 감사! – NinjaShawarma