我是JavaScript的新手,并开始做一个像这样工作的小应用程序:
我的应用程序需要时间作为输入,并且应该与当前时间匹配
如果匹配则会显示通知
这是接受输入的代码,但它不是显示通知,我没有正确构建我的代码,因为我是JavaScript的新手。 并帮助我如何使用PM和AM进行显示。
<!DOCTYPE HTML>
<html>
<body>
Current Time<p id="CurrentTime"></p>
Enter The Time <input type="text" id="textbox">
<button onclick="notifyMe()">Notify!</button>
<p id='input' >hello</p>
<p id="time"></p>
<script type="text/javascript">
window.setTimeout("ShowTime()", 1000);
</script>
<script type="text/javascript">
var d1 = new Date();
var d2 = new Date();
var tempcheck=null;
alert("Showing time");
function ShowTime() {
var temptime="15:27";
var d1 = new Date();
var d2 = new Date();
d1.setHours(+d2.getHours());
d1.setMinutes(new Date().getMinutes());
var ftime=d1.getHours()+":"+d1.getMinutes()+":"+d1.getSeconds();
var tempcheck=d1.getHours()+":"+d1.getMinutes();
document.getElementById("CurrentTime").innerHTML = ftime;
window.setTimeout("ShowTime()", 1000);
} //Showtime ends
</script>
<script>
var take_input=null;
var d1 = new Date();
var d2 = new Date();
var ftimec=null;
function CheckTime(take_input){
d1.setHours(+d2.getHours());
d1.setMinutes(new Date().getMinutes());
var ftimec=d1.getHours()+":"+d1.getMinutes();
document.getElementById('time').innerHTML=ftimec;
if (take_input==ftimec) {
if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification("Remainder Time Has Reached "+take_input);
}
} else {
setTimeout(function() { CheckTime(take_input);}, 1000)
}
}
</script>
<script>
function notifyMe() {
var input = document.getElementById("textbox").value;
//below displays the entered time by user
document.getElementById('input').innerHTML=input;
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification("Remainder Set at "+input);
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification("Denied");
}
});
}
window.setTimeout("CheckTime(input)",1000);
}
</script>
</body>
</html>