简体   繁体   English

有人可以告诉我这个JS警报有什么问题吗

[英]can someone tell me what is wrong with this JS alarm

I'm trying to make a simple alarm to practice I think the issue is with the time input, yet I can not detect where is the problem last thing I tried is to slice the innerHTML string match it with if :D is problem with interval or the matching with if I don't know I'm still beginner, thanks in advance 我正在尝试发出一个简单的警报来练习,我认为问题出在时间输入上,但我无法检测出问题出在哪里最后我想尝试的是切入与之匹配的innerHTML字符串,如果:D是间隔问题或如果我不知道我还是初学者的话,请提前谢谢

 var dateId = document.getElementById("date") var timeId = document.getElementById("time") var date = new Date(); dateId.innerHTML = date; function myTimer() { var d = new Date(); document.getElementById("time").innerHTML = d.toLocaleTimeString().slice(0, 8); } setInterval(myTimer, 1000); document.getElementById("Stime").addEventListener("input", SetTime); function SetTime() { var input = this.value; console.log(input) return input; } var inputValue = SetTime(); function matchTime() { if (inputValue === timeId.innerHTML) { console.log("Alarm") } } setInterval(matchTime, 1000); console.log(inputValue); //it gives me undefined 
 <div id="date"> </div> <div id="time"> </div> <br> <br> <br> <input id="Stime" type="time" step="1" name="appt-time" value="13:30"> <div id="SLtime"> </div> <div id="SPtime"> </div> 

The first time you run SetTime() right here: 第一次在此处运行SetTime()

var inputValue = SetTime();

this.value inside SetTime() is undefined, since it is being called by the window. SetTime() this.value是未定义的,因为它是由窗口调用的。 When it is later called as an event on the <input> , it gives you the value you expect. 以后在<input>上将其作为事件调用时,它将为您提供期望的值。 Have the <input> call the function instead and you will get its value. <input>代替调用该函数,您将获得它的值。

var stime = document.getElementById("Stime");
var inputValue = SetTime.call(stime);

Here's the full snippet: 这是完整的代码段:

 var dateId = document.getElementById("date") var timeId = document.getElementById("time") var date = new Date(); dateId.innerHTML = date; function myTimer() { var d = new Date(); document.getElementById("time").innerHTML = d.toLocaleTimeString().slice(0, 8); } setInterval(myTimer, 1000); document.getElementById("Stime").addEventListener("input", SetTime); function SetTime() { var input = this.value; console.log(input) return input; } var stime = document.getElementById("Stime"); var inputValue = SetTime.call(stime); function matchTime() { if (inputValue === timeId.innerHTML) { console.log("Alarm") } } setInterval(matchTime, 1000); console.log(inputValue); //it gives me undefined 
 <div id="date"> </div> <div id="time"> </div> <br> <br> <br> <input id="Stime" type="time" step="1" name="appt-time" value="13:30"> <div id="SLtime"> </div> <div id="SPtime"> </div> 

Actually, when you change the alarm value it's only got the hours and minutes from it until you changed the seconds manually. 实际上,当您更改警报值时,它只有小时和分钟,直到您手动更改秒。 And we are comparing the timer (which contains hours, minutes and seconds) and alarm value which contains only hours and minutes that's why Alarm is not print. 我们正在比较计时器(包含小时,分钟和秒)和仅包含小时和分钟的警报值,这就是为什么不打印警报的原因。

I just add ":00" for the seconds in the timer and compare its with alarm value. 我只是在计时器中添加秒数“:00”,并将其与警报值进行比较。

 var dateId = document.getElementById("date") var timeId = document.getElementById("time") var date = new Date(); var setTime; // used for set the alerm time value. dateId.innerHTML = date; function myTimer() { var d = new Date(); document.getElementById("time").innerHTML = d.toLocaleTimeString().slice(0, 8); } setInterval(myTimer, 1000); document.getElementById("Stime").addEventListener("input", function (evt) { // Add :00 in value for seconds. setTime = this.value.length == 5 ? (this.value+':00') :this.value; // set the setTime (alarm value) }); function matchTime() { // check the alarm value with our timer if (setTime=== timeId.innerHTML) { console.log("Alarm") } } setInterval(matchTime, 1000); console.log(setTime); 
 <div id="date"> </div> <div id="time"> </div> <br> <br> <br> <input id="Stime" type="time" step="1" name="appt-time" value="13:30" min="00:00:00"> <div id="SLtime"> </div> <div id="SPtime"> </div> 

Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人能告诉我我做错了什么 - Can someone tell me what am i doing wrong Ace编辑器-能否有人告诉我require()有什么问题? - Ace Editor - Can please someone tell me what is wrong with require()? 有人能告诉我这个java脚本有什么问题吗? - Can someone tell me what is wrong with this java script? 有人可以告诉我此javascript有什么问题吗? - Can someone tell me what's wrong with this javascript? 有人可以告诉我这小段JS代码在做什么吗? - Can someone tell me what this small chunk of js code is doing? Discord.js - 类型错误:无法读取未定义的属性“声音”。 有人可以告诉我我做错了什么吗? - Discord.js - TypeError: Cannot read property 'voice' of undefined. Can someone tell me what I'm doing wrong? 我正在学习编码,有人可以告诉我哪里错了吗? - I am learning to code, can someone please tell me what is wrong? 有人能告诉我这里出了什么问题吗? 我想用大写字母返回蔬菜数组 - Can someone tell me what is wrong here? I want to return the vegetable array in CAPITAL letters 有人可以告诉我我的代码有什么问题吗? 返回正确的世纪 - Can someone tell me what's wrong with my code? Return correct century JavaScript:有人可以告诉我我的代码出了什么问题吗? - JavaScript: Can someone tell me what's going wrong with my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM