$ .getJSON + setInterval的

问题描述:

我得到下面的代码从JSONP一些数据文件,通过:

$.getJSON('http://static.eska.pl/m/playlist/channel-108.jsonp?callback=?'); 
 
function jsonp(data) { 
 
document.getElementById("artist").innerHTML = data[0].artists[0].name; 
 
document.getElementById("title").innerHTML = data[0].name; 
 
};
<!DOCTYPE html> 
 
<head> 
 
    <title>JSONP EskaRock </title> 
 
\t <script src="http://code.jquery.com/jquery-latest.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
</head> 
 

 
<body> 
 
<div id="artist"></div> 
 
<div id="title"></div> 
 
</body> 
 
</html>

它的工作原理,但我需要更新数据,每10秒。我使用setInterval函数,但控制台FireFox返回错误“ReferenceError:jsonp未定义 (... channel-108.jsonp:1:1)”。我在使用setInterval代码:

setInterval(function() { 
 
$.getJSON('http://static.eska.pl/m/playlist/channel-108.jsonp?callback=?'); 
 
function jsonp(data) { 
 
document.getElementById("artist").innerHTML = data[0].artists[0].name; 
 
document.getElementById("title").innerHTML = data[0].name; 
 
}; 
 
}, 10000)
<!DOCTYPE html> 
 
<head> 
 
<title>JSONP EskaRock </title> 
 
<script src="http://code.jquery.com/jquery-latest.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
</head> 
 

 
<body> 
 
<div id="artist"></div> 
 
<div id="title"></div> 
 
</body> 
 

 
</html>

在哪里的问题?

您声明内setInterval外移动它,它会工作

function jsonp(data) { 
 
    document.getElementById("artist").innerHTML = data[0].artists[0].name; 
 
    document.getElementById("title").innerHTML = data[0].name; 
 
}; 
 
setInterval(function() { 
 
    $.getJSON('http://static.eska.pl/m/playlist/channel-108.jsonp?callback=?'); 
 

 
}, 10000)
<!DOCTYPE html> 
 

 
<head> 
 
    <title>JSONP EskaRock </title> 
 
    <script src="http://code.jquery.com/jquery-latest.js"></script> 
 

 
</head> 
 

 
<body> 
 
    <div id="artist"></div> 
 
    <div id="title"></div> 
 
</body>

功能