仅在周末触发JS功能?

时间:2016-01-07 11:52:34

标签: javascript function date

我想创建一个仅在周末(周六和周日)触发的功能。

查看我的JSfiddle here

也许我可以使用以下代码:

var d = new Date(); 
var weekday = new Array(7);
weekday[0]=  "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";

var n = weekday[d.getDay()];

2 个答案:

答案 0 :(得分:1)

你可以这样做

var d = new Date(); 
if (curday == 'Saturday' || curday == 'Sunday') { 
  var weekday = new Array(7);
      weekday[0]=  "Sunday";
      weekday[1] = "Monday";
      weekday[2] = "Tuesday";
      weekday[3] = "Wednesday";
      weekday[4] = "Thursday";
      weekday[5] = "Friday";
      weekday[6] = "Saturday";

      var n = weekday[d.getDay()];
} 

答案 1 :(得分:0)

这是最适合我的最终代码。

<script>
$("strong:contains('Beställ innan kl 16.00')").attr("id", "countdown").delay(1000);
    function ShowTime() {
      var now = new Date();
      var offset = now.getTimezoneOffset() / 60;

      now.setHours(now.getHours() + (offset+1));
      if (now.getHours() < 16) {
        var hrs = 15-now.getHours();
        var mins = 59-now.getMinutes();
        var secs = 59-now.getSeconds();
        if (hrs < 10) hrs = "0" + hrs;
        if (mins < 10) mins = "0" + mins;
        if (secs < 10) secs = "0" + secs;
        timeLeft = 'Beställ inom <span style="color: #ff713d; font-size: larger">' +hrs+'t '+mins+'m '+secs+'s</span>';
        $("#countdown").html(timeLeft);
        $("span:contains('så skickas din beställning samma vardag')").html("<br />och varan skickas idag.")
      }
    }


var day = new Date().getDay(); 
if (day != 0 && day != 6){
  setInterval(ShowTime, 1000);
}