2016-02-11 128 views
1

我知道JavaScript并不是最好的方法去做这件事。我知道我必须启用浏览器并始终运行。我通常会用Python做些事情。这是我的具体要求,我不是很熟练使用JavaScript。话虽如此。使用Javascript我希望能够在特定时间运行特定任务

我希望用户能够使用输入设置时间。一旦设置了这些输入,我希望浏览器检查指定的时间。一旦时间到了,我希望它执行一个命令。

她是我到目前为止有:

<html> 
<body> 

<p>Enter Time to start dashboard</p> 
<p>Hour</p> 
<input id="strthour"> 
<p>Minute</p> 
<input id="strtmin"> 
<button onclick="setTime()">Submit</button> 

<script> 
var hr = 06; //default time of 6am to run 
var mn = 00; 

function setTime() { 
    hr = strthour.value; 
    mn = strtmin.value; 
} 

window.setInterval(function(){ // Set interval for checking 
    alert(hr+mn); 
    var date = new Date(); // Create a Date object to find out what time it is 
    if(date.getHours() === hr && date.getMinutes() === mn && date.getSeconds() === 0){ // Check the time 
     alert("it worked") 
    } 
}, 5000); // Repeat every 60000 milliseconds (1 minute) 

</script> 

</body> 
</html> 

我能够改变全局变量,但我无法得到window.setInterval认识的变化。有什么建议?

这是我制作的JSFiddle的链接。

+0

我不确定你的意思是“我无法获得window.setInterval来识别这些变化。”你能更具体地了解发生了什么问题吗?我预见到的一个问题是,你正在检查'date.getSeconds === 0'',但'setInterval'每5秒运行一次。 – jered

+0

当我使用setTime()函数更改全局变量hr和mm时,window.setInterval无法识别更改。我希望能够使用setTime()更改全局变量,并让window.setInterval使用它们的值在特定时间运行命令。 – Ryan113

回答

1

您没有正确指输入,你也有你的警觉语法错误。下面是我建议的修复(工作):

<p>Enter Time to start dashboard</p> 
<p>Hour</p> 
<input id="strthour"> 
<p>Minute</p> 
<input id="strtmin"> 
<button onclick="setTime()">Submit</button> 
<script> 
    var hr = 0; 
    var mn = 0; 

    function setTime() { 
     hr = parseInt(document.getElementById("strthour").value); 
     mn = parseInt(document.getElementById("strtmin").value); 
     console.log("set time: "+hr+":"+mn); 
    } 

    setInterval(function(){ 
     var date = new Date(); 
     if(date.getHours() == hr && date.getMinutes() == mn){ // using == not === 
      alert("it worked"); 
     } 
    }, 10000); 
</script> 

注意:您也应该parseInt函数()的值,以确保它们是有效的数字。

+1

您还应该使用“==”运算符,而不是“===” – TheMintyMate

+0

正确,字符串int比较将永远不会出现在'===' – walkerrandophsmith

+0

谢谢。这给了我我需要的东西。 – Ryan113

1

这不是因为setInterval无法识别更改,您实际上不会修改这些值。

如果您在jsfiddle页面上打开javascript控制台,您会看到“Uncaught ReferenceError:setTime未定义”。

如果你定义你setTime这样它将工作:

window.setTime = function() { 
    hr = strthour.value; 
    mn = strtmin.value; 
} 

这是因为的jsfiddle不直接运行你的代码,但包装成

<script type='text/javascript'>//<![CDATA[ 
window.onload=function(){ 
    ... // you code here } 
}//]]> 

这里是一个modified JSFiddle刚刚“它为我工作”。

更新 - 一些笔记,在其他的答案中提到:

  • 使用“===”也是一个问题,人力资源/分钟都是字符串,所以你需要“==”或将hr/mn转换为整数
  • 表达式strthour.valuesetTime中可用于JSFiddle。我不太确定为什么,但它有效。在 “现实世界”,它应该像document.getElementById("strthour").value

更新2 - 为什么strthour.value工作(VS document.getElementById("strthour").value)?

这实际上对我来说是一个惊喜,但它看起来像所有主流浏览器都将所有元素与id放入窗口对象中。更重要的是,它实际上是HTML standard的一部分(虽然不建议使用此功能):

6.2.4 Named access on the Window object

window[name] Returns the indicated element or collection of elements.

As a general rule, relying on this will lead to brittle code. Which IDs end up mapping to this API can vary over time, as new features are added to the Web platform, for example. Instead of this, use document.getElementById() or document.querySelector().

参考文献:

-1

我觉得你应该用“ > =“运营商,因为你不知道这是否是时间。

1
if(date.getHours() === hr && date.getMinutes() === mn && date.getSeconds() === 0){ // Check the time 
     alert("it worked") 
    } 

这会将一个字符串与一个int进行比较,并始终为false。

要么执行parseInt(date.getHours())或使用==

1

你的代码有几个问题,各个人都指出了。

  1. 沃克伦道夫·史密斯正确地指出,date.GetHours()date.getMinutes()都将返回数字,而从价值观和strthour.value返回strtmin.value将字符串。当JavaScript比较这两个时,它将始终评估为false。要解决此问题,请尝试通过parseInt运行用户输入,如hr = parseInt(strthour.value, 10);。 10很重要,因为它告诉parseInt创建一个基数10(你不需要知道这意味着什么,只要确保包含10)。

  2. 您需要秒匹配可能是不必要的,并且与您选择的间隔不匹配。 TheMintyMate在他们的代码片段中通过简单地删除比较秒来做出这种更正。如果您确实需要确保秒数完美匹配,请选择少于1000毫秒的时间间隔,以便您知道它将每秒至少检查一次,以确保您将在所需的第0秒内运行检查时间。

  3. ,如果你试图将它们的比较结果为字符串,而不是像点1.推荐.getMinutes()方法将返回一个数字0像6时间转换为数字你可能会遇到一些麻烦与单数的分钟数: 00,而您的示例隐式提示用户在同一时间输入两位数字。同样,您可以完全避免使用parseInt,正如第1点中所建议的那样。

我必须抛出一个插件,使用Cron作业来按照像这样的已知时间表运行任务。我知道你说用户在这种情况下要求JS,所以他们可能不会申请这个特定的情况。由于你没有提到Cron工作,所以我必须将它们包含在这里,以确保你和未来的读者都能意识到它们,因为它们是专门用于按照自动时间表运行任务的情况。

祝你好运!