好吧,JavaScript 中的简单问题我什至在网络上搜索都无法解决。我想我什至找到了正确的东西,但无法放在正确的地方。
此代码告诉我流是在线还是离线。但是我该如何处理状态并保持每 5 秒更新一次呢?
$(function () {
$.ajax({
type: 'GET',
url: "http://xmychannelx.api.channel.livestream.com/2.0/livestatus.json?callback=?",
dataType: 'jsonp',
success: function (jsonp) {
// parse the JSON data on success
var channel = eval(jsonp);
liveChannel = channel.channel.isLive;
if (liveChannel == true) {
document.getElementById('mydiv').innerHTML = '<p style="color: #00FF00">Online!</p>';
} else {
document.getElementById('mydiv').innerHTML = '<p style="color: #C0C0C0">Offline!</p>';
}
}
});
});
最佳答案
示例:
var myAjaxCall = function() {
$.ajax({
type: "GET",
url: options.feedUrl,
dataType: "xml",
async:options.sync,
success: function(xml) {
// todo
}
};
var ResInterval = window.setInterval(myAjaxCall, 60000); // 60 seconds
停止:
window.clearInterval(ResInterval);
关于JavaScript 状态循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14805213/