gpt4 book ai didi

javascript - 自动刷新 json 无需重新加载

转载 作者:行者123 更新时间:2023-12-03 08:07:40 26 4
gpt4 key购买 nike

我正在尝试制作一个自动刷新 json,它每 5 秒重新加载一次更改。它第一次加载时完美加载,但我的 setinterval 无法正常工作。它每 5 秒更新一次,但即使已进行更改,它也不会更新我的菜单? 。

这是我到目前为止得到的:

 $(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);
});

});

我使用此代码,但刷新时他给出了我的空页面

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
});

关于javascript - 自动刷新 json 无需重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34310319/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com