在特定时间调用javascript函数 [英] Call a javascript function at a specific time of day

查看:94
本文介绍了在特定时间调用javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如我想在上午10.00.00.00调用js函数
我该怎么办?

for example i want to call a js function at 10.00.00.00 am how can i do?

<script type="text/javascript">

var now = new Date();

var millisTill10 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 30, 0, 0) - now;

setTimeout(function{openAPage(), setInterval(openAPage, 60*1000)}, millisTill10)

function openAPage() {

var startTime = new Date().getTime();
var myWin = window.open("http://google.com","_blank")
var endTime = new Date().getTime();
var timeTaken = endTime-startTime;


document.write("<br>button pressed@</br>")
document.write(new Date(startTime));
document.write("<br>page loaded@</br>")
document.write(new Date(endTime));
document.write("<br>time taken</br>")
document.write(timeTaken);

myWin.close()

}

</script>

我希望这个代码在00.30开启谷歌,然后每隔1分钟它会做到这一点再次?那个代码怎么了?

i expect from this code at 00.30 it will open google and then every 1 minute later it will do it again? whats wrong with that code?

推荐答案

你需要 setTimeout 设置一个计时器和日期计算计时器触发前需要多长时间。

You'll need setTimeout to set a timer and Date to calculate how long the timer needs to go until it triggers.

var now = new Date();
var millisTill10 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 10, 0, 0, 0) - now;
if (millisTill10 < 0) {
     millisTill10 += 86400000; // it's after 10am, try 10am tomorrow.
}
setTimeout(function(){alert("It's 10am!")}, millisTill10);

这篇关于在特定时间调用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆