2017-12-02 129 views
0

我得到下面的代碼從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>

在哪裏的問題?

回答

0

您聲明內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>

功能