无需重新加载即可自动刷新json [英] auto refresh json without reload
本文介绍了无需重新加载即可自动刷新json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试制作一个自动刷新 json,它每 5 秒重新加载一次以进行更改.它在第一次加载时完美加载,但我的 setinterval 无法正常工作.它每 5 秒运行一次,但即使进行了更改,它也不会更新我的菜单?.
i am trying to make an auto refresh json which is reloading for changes every 5 seconds. It loads perfectly first time on load but my setinterval is not working correctly. It goes of every 5 seconds but it doesnt update my menu even though changes has been made? .
这是我目前得到的:
$(document).ready(function(load) {
var dmJSON = "example.com";
$.getJSON( dmJSON, function(data) {
setInterval(function () {
$(news).html("");
$.getJSON();
}, 5000);
var html = '';
// loop through all the news items, and append the
// title and published date to the html variable.
for(var i = 0; i < data.feed.data.length; i++){
html += '<div style="width: 600;direction: rtl;background-color: white;padding: 12px 12px 0;border: 1px solid;border-color: #e5e6e9 #dfe0e4 #d0d1d5;border-radius: 4px;margin-bottom: 20px;color: black;">';
html += '<span style="/* text-align: left; */direction: rtl;position: absolute;left: 250px;"> ' + data.feed.data[i].created_time + ' </span><span><img alt="" src="http://icons.iconarchive.com/icons/gakuseisean/ivista-2/128/Network-Globe-Disconnected-icon.png" style="background-size:auto;background-repeat: no-repeat;display: inline-block;height: 20px;width: 20px;position: absolute;left: 490px;padding-top: 9px;" /></span>' ;
html += '<br>' ;
html += data.feed.data[i].message ;
html += '<p><img alt="" src=' + data.feed.data[i].picture + ' /></p>';
html += '</div>';
}
// append all the html variable to the ticker div.
$("#news").append(html);
});
});
我使用此代码,但刷新时他给了我的空白页面
i use this code but when refresh he give my empty page
setInterval(function () {
$(news).html("");
$.getJSON();
}, 5000);
推荐答案
试试这个:
$(document).ready(function(load) {
var dmJSON = "https://";
function fetch() {
$.getJSON( dmJSON + (+new Date()), function(data) {
$("#news").html('');
var html = '';
// loop through all the news items, and append the
// title and published date to the html variable.
for(var i = 0; i < data.feed.data.length; i++){
html += '<div style="width: 600;direction: rtl;background-color: white;padding: 12px 12px 0;border: 1px solid;border-color: #e5e6e9 #dfe0e4 #d0d1d5;border-radius: 4px;margin-bottom: 20px;color: black;">';
html += '<span style="/* text-align: left; */direction: rtl;position: absolute;left: 250px;"> ' + data.feed.data[i].created_time + ' </span><span><img alt="" src="http://icons.iconarchive.com/icons/gakuseisean/ivista-2/128/Network-Globe-Disconnected-icon.png" style="background-size:auto;background-repeat: no-repeat;display: inline-block;height: 20px;width: 20px;position: absolute;left: 490px;padding-top: 9px;" /></span>' ;
html += '<br>' ;
html += data.feed.data[i].message ;
html += '<p><img alt="" src=' + data.feed.data[i].picture + ' /></p>';
html += '</div>';
}
// append all the html variable to the ticker div.
$("#news").append(html);
});
}
setInterval(fetch, 5000);// call fetch every 5 seconds
fetch(); // call fetch first time
});
这篇关于无需重新加载即可自动刷新json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文