如何每小时自动调用javascript函数?
我有一个javascript函数,可以从URL获取JSON对象,我希望每小时用最新数据刷新JSON对象 我假设这样做的方法是调用javascript函数,该函数每小时从URL获取JSON对象 然而,我不知道如何设置一个功能,以自动重新运行每小时,有人可以解释这是如何做到的 谢谢您可以尝试:如何每小时自动调用javascript函数?,javascript,jquery,ajax,json,Javascript,Jquery,Ajax,Json,我有一个javascript函数,可以从URL获取JSON对象,我希望每小时用最新数据刷新JSON对象 我假设这样做的方法是调用javascript函数,该函数每小时从URL获取JSON对象 然而,我不知道如何设置一个功能,以自动重新运行每小时,有人可以解释这是如何做到的 谢谢您可以尝试: function doSomething() { alert('Test'); } setInterval(doSomething, 60*60*1000); setInterval(funct
function doSomething()
{
alert('Test');
}
setInterval(doSomething, 60*60*1000);
setInterval(function(){alert("Hello")},3600000);
其中3600000是以毫秒为单位的时间间隔。您可以尝试:
setInterval(function(){alert("Hello")},3600000);
其中3600000是以毫秒为单位的时间间隔。使用
setInterval
如下所示:-
语法
setInterval(function,milliseconds,lang)
参数值
Parameter Description
function Required. The function that will be executed
milliseconds Required. The intervals (in milliseconds) on how often to execute the code
lang Optional. JScript | VBScript | JavaScript
返回值
function test()
{
////Your code
}
setInterval(function(){test()}, 60 * 60 *1000); //// 60 minutes = 3600 seconds = 3600000 miliseconds
一个整数,具有设置的计时器的ID值。将此值与clearInterval()方法一起使用可取消计时器
示例
function test()
{
////Your code
}
setInterval(function(){test()}, 60 * 60 *1000); //// 60 minutes = 3600 seconds = 3600000 miliseconds
有关详细信息:-
使用
设置间隔
,如下所示:-
语法
setInterval(function,milliseconds,lang)
参数值
Parameter Description
function Required. The function that will be executed
milliseconds Required. The intervals (in milliseconds) on how often to execute the code
lang Optional. JScript | VBScript | JavaScript
返回值
function test()
{
////Your code
}
setInterval(function(){test()}, 60 * 60 *1000); //// 60 minutes = 3600 seconds = 3600000 miliseconds
一个整数,具有设置的计时器的ID值。将此值与clearInterval()方法一起使用可取消计时器
示例
function test()
{
////Your code
}
setInterval(function(){test()}, 60 * 60 *1000); //// 60 minutes = 3600 seconds = 3600000 miliseconds
有关详细信息:-
您可以通过设置间隔重新调用函数:
<script type="text/javascript">
function getJSONObjectFromURL(){
// do stuff
}
//set interval in milliseconds and call function again
//1h = 60m = 3600s = 3600000ms
var timeoutID = window.setInterval(getJSONObjectFromURL, 3600000);
</script>
函数getJSONObjectFromURL(){
//做事
}
//以毫秒为单位设置间隔,然后再次调用函数
//1h=60m=3600s=3600000ms
var timeoutID=window.setInterval(getJSONObjectFromURL,3600000);
您可以通过设置间隔重新调用函数:
<script type="text/javascript">
function getJSONObjectFromURL(){
// do stuff
}
//set interval in milliseconds and call function again
//1h = 60m = 3600s = 3600000ms
var timeoutID = window.setInterval(getJSONObjectFromURL, 3600000);
</script>
函数getJSONObjectFromURL(){
//做事
}
//以毫秒为单位设置间隔,然后再次调用函数
//1h=60m=3600s=3600000ms
var timeoutID=window.setInterval(getJSONObjectFromURL,3600000);
客户端?您可以使用setInterval
,这要求您的页面始终处于打开状态。阅读setInterval-但是谁的网页打开了一个小时?您可以使用cron作业查看此URL,假定AJAX使用服务器端语言(PHP或其他语言),将上次刷新的时间存储在数据库中。然后,在请求JSON对象之前,检查存储的上次更新时间是否已经过去了一个小时。每个写答案的人是否至少都知道正确的毫秒数?客户方?您可以使用setInterval
,这要求您的页面始终处于打开状态。阅读setInterval-但是谁的网页打开了一个小时?您可以使用cron作业查看此URL,假定AJAX使用服务器端语言(PHP或其他语言),将上次刷新的时间存储在数据库中。然后,在请求JSON对象之前,检查存储的上次更新时间是否已经过去了一个小时。每个写答案的人是否至少都知道正确的毫秒数?Sheesh.时间单位是毫秒,而不是秒。时间单位是毫秒,而不是秒。在setInterval
中使用字符串是XSS问题。在setInterval
中使用字符串是XSS问题。我们错过了下面4个完全相同的答案,是吗?是的,你是对的,它们不在那里,当我开始输入答案时。对我来说,这不是投票反对的理由!我很抱歉,但我在给出答案时没有看到它…错过了下面4个完全相同的答案,是吗?是的,你是对的,当我开始键入答案时,它们不在那里。对我来说,这不是投票反对的理由!对不起,我在回答时没有看到它。。。