【发布时间】:2015-05-28 11:34:52
【问题描述】:
我有以下脚本:
var results;
var cursor = 0;
function myFunction () {
$.getJSON('list.php', function(json) {
results = json.result;
cursor = 0;
// Now start printing
printNext();
});
}
function printNext(){
if(cursor == results.length){
// Reset the cursor back to the beginning.
cursor = 0;
}
// Print the key1 in the div.
//$('#device-content-user-text').html(results[cursor].key1);
$('#device-content-user-text').hide('fast', function(){ $('#device-content-user-text').html(results[cursor].key1); $('#device-content-user-text').show('fast'); });
// Set a delay for the current item to stay
// Delay is key2 * 1000 seconds
setTimeout(function(){
printNext();
}, results[cursor].key2 * 1000);
// Advance the cursor.
cursor++;
}
var interval = setInterval(function () {
myFunction();
}, 300000); //make sql query every 5 minutes
它从页面 list.php 中获取 JSON 字符串,并在 #device-content-user-text div 中一一打印结果。它每五分钟完成一次,当用户加载页面时,计时器开始计时。如何在页面加载时(通常每 5 分钟一次)调用此函数?
谢谢
【问题讨论】:
-
您的
setInterval只调用myFunction(),所以只需在间隔之外调用myFunction()。 -
你可以在文件准备好时调用它:
$(myFunction);
标签: javascript jquery