日出闹钟 - 网站

时间:2017-11-12 15:32:05

标签: javascript html css web

我的朋友有一个日出闹钟"当它醒来时变得更加明亮,以模拟日出以轻松醒来。

我认为我可以用我的笔记本电脑做同样的事情,但是我一直在努力让Javascript有时间工作。我很感激能帮助你做这件事。

我一直试图添加分钟值(wakeupM),但是我想要睡觉多久,这会延迟setTimeout功能,只有在我醒来时才会运行。不过,我希望能够在时间输入框和" sunrise"中设置时间。功能仅在达到该时间时运行。

提前感谢您的帮助!



var countTime;

function mySleep() {
  var now = new Date();
  var nowM = now.getMinutes();


  var wakeup = new Date();
  var wakeupM = wakeup.getMinutes() + 3;


  var timeDif = wakeupM - nowM;
  countTime = 6000 * timeDif;


  document.getElementById("settime").innerHTML = countTime;

}

function mySunrise() {
  var colors = ['orange', 'yellow', 'white'];
  var active = 0;


  setInterval(function() {
    document.querySelector('body').style.background = colors[active];
    active++;
    if (active == colors.length) active = 0;
  }, 3000);;
}

body {
  background: green;
  /* initial color */
  transition: background 5s;
  /* .5s how long transition should take */
}

<input type="time" id="myTime" value="22:15:00">
<button onclick="setTimeout(mySunrise, countTime); mySleep();">Wake me up</button>
<h1>waking you up at:</h1>
<p id="settime"></p>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我的个人网站 - http://piotrirving.com/alarm/sunrise.html

上的完成版本

    char[] arr = "abcdefg".toCharArray();
    List<Character> list = new LinkedList<>(); // copy the chars to a list
    for (int i = 0; i < arr.length; i++) {
        list.add(arr[i]);
    }
    Collections.shuffle(list);  // use to shuffle
    for (int i = 0; i < arr.length; i++) { // copy the shuffled chars back to the array
        arr[i] = list.get(i);
    }
    System.out.println(new String(arr));
var wakeupH;
var wakeupM;
var wakeupS;

var nowH;
var nowM;
var nowS;


function mySleep() {
  var wakeUpTime = document.getElementById("myTime");
  var currentVal = wakeUpTime.value;
  var res = currentVal.split(":");

  wakeupH = res[0]
  wakeupM = res[1]
  wakeupS = res[2]

  var now = new Date();
  var nowH = now.getHours();
  var nowM = now.getMinutes();
  var nowS = now.getSeconds();

  var date1 = new Date(2000, 0, 1, nowH, nowM, nowS);

  var date2 = new Date(2000, 0, 1, wakeupH, wakeupM, 1);


  if (date2 < date1) {
    date2.setDate(date2.getDate() + 1);
  }

  countTime = date2 - date1;
  document.getElementById("settime").innerHTML = countTime;
}



function mySunrise() {
  var colors = ["#BDB76B", "#F0E68C", "#EEE8AA", "#FFDAB9", "#FFE4B5", "#FFA07A", "#FF7F50", "#FF6347", "#FF4500", "#FF8C00", "#FFA500", "#FFEFD5", "#FAFAD2", "#FFFACD", "#FFFFE0", "#FFFF00", "#FFD700", "white"];
  var active = 0;


  setInterval(function() {
    document.querySelector('body').style.background = colors[active];
    active++;
    if (active == colors.length) active = 0;
  }, 3000);;
}
body {
  background: black;
  /* initial color */
  transition: background 30s;
  /* .5s how long transition should take */
  color: white;
}