大家好我有 SetInterval
我've try using it like this to refresh my request but it'的问题不能正常工作
setInterval(test, 10000);
$.get("https://ipinfo.io",
function test(response) {
console.log(response.ip, response.country);
var body = document.getElementsByTagName('body')[0]
var creatediv = function(s1, s2, s3, s4) {
var div = document.createElement('div');
div.appendChild(s1);
div.appendChild(s2);
return div;
}
var createspan = function(value) {
var span = document.createElement('span');
span.innerText = value;
return span;
}
body.appendChild(creatediv(createspan('Your IP adress: '), createspan(response.ip)))
body.appendChild(creatediv(createspan('Your City: '), createspan(response.city)))
body.appendChild(creatediv(createspan('Country: '), createspan(response.country)))
body.appendChild(creatediv(createspan('Postal Code: '), createspan(response.postal)))
}, "jsonp")
2 回答
你需要在你的间隔回调中调用
$.get()
:您遇到的第一个问题是您的刷新永远不会刷新 . 据推测,您希望每秒从ipinfo获取最新数据,然后更新显示以显示该信息 .
设置的
test()
函数仅包含更新显示所需的逻辑 - 而不是获取数据 .下一个问题是他
test()
在全球范围内可以使用're trying to call in setInterval is created as a closure function, which isn' .您可以自行定义
test()
来更正此问题 .结合这两个修复程序将允许setInterval调用服务器并操纵页面 .