您好,我希望为我的getJson函数设置一个30秒的setinterval,任何提示都会得到慷慨接收......
我的代码如下......
$(function()
{
$(document).ready(function()
{
$.getJSON("tempdata.json",function(data)
{
$.each(data.recent, function(i,data){
var div_data ="<div class='row'><div class='col-sm-12 rowpost'><div class='col-sm-3'><div class='postplace'><img src='"+data.coverurl+"' width='80' height='50'></img></div></div><div class='col-sm-9'> <span class='post_title1'>"+data.trackartist+"</span><br> <small1><em>"+data.tracktitle+"</em></small1></div></div></div>";
$(div_data).appendTo("#Tracksinner");
});
}
);
return false;
});
});
答案 0 :(得分:3)
您需要对您的函数进行封装,以便在每个时间间隔内获取数据。
要进行初始运行,请调用函数update_trackdata()
之后,间隔将接管。
$(document).ready(function() {
function update_trackdata() {
$.getJSON("tempdata.json", function(data) {
var div_data = "";
$.each(data.recent, function(i, data) {
div_data += "<div class='row'><div class='col-sm-12 rowpost'><div class='col-sm-3'><div class='postplace'><img src='" + data.coverurl + "' width='80' height='50'></img></div></div><div class='col-sm-9'> <span class='post_title1'>" + data.trackartist + "</span><br> <small1><em>" + data.tracktitle + "</em></small1></div></div></div>";
("#Tracksinner");
});
$("#Tracksinner").html(div_data);
});
return false;
}
update_trackdata();
var interval = setInterval(update_trackdata, 30000);
});
答案 1 :(得分:1)
使用setTimeout进行等待.....
尝试
$.getJSON("tempdata.json", function (data) {
setTimeout(function () {
$.each(data.recent, function (i, data) {
var div_data = "<div class='row'><div class='col-sm-12 rowpost'><div class='col-sm-3'><div class='postplace'><img src='" + data.coverurl + "' width='80' height='50'></img></div></div><div class='col-sm-9'> <span class='post_title1'>" + data.trackartist + "</span><br> <small1><em>" + data.tracktitle + "</em></small1></div></div></div>";
$(div_data).appendTo("#Tracksinner");
});
}, 30000);
});
答案 2 :(得分:1)
var timer;
timer = setInterval(function() {
// Your get JSON call
}, 30 * 1000);