简体   繁体   English

设置间隔ajax请求

[英]Set interval ajax request

Hello guys i have a problem with SetInterval i've try using it like this to refresh my request but it's not working 大家好,我遇到了SetInterval的问题,我已经尝试过使用它来刷新我的请求,但是它不起作用

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")

You need to call $.get() inside your interval callback: 您需要在间隔回调内调用$.get()

 setInterval(test, 1000); function test() { $.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"); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

The first issue you have is that your refresh will never refresh. 您遇到的第一个问题是刷新将永远不会刷新。 Presumably, you want to fetch the latest data from ipinfo every second, then update your display to show that information. 假设您想每秒从ipinfo获取最新数据,然后更新显示以显示该信息。
The test() function as it's set up contains only the logic needed to update the display - not to fetch data. 设置的test()函数仅包含更新显示所需的逻辑-不获取数据。

The next issue is that he test() function you're trying to call in setInterval is created as a closure function, which isn't available in the global scope. 下一个问题是,您尝试在setInterval中调用的test()函数被创建为闭包函数,该函数在全局范围内不可用。
You can define test() on its own to correct this. 您可以单独定义test()来更正此问题。

Combining those two fixes will allow setInterval to call the server and manipulate the page. 结合这两个修复程序,可以使setInterval调用服务器并操纵页面。

function test() {
    $.get("https://ipinfo.io",
        function (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")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM