0

我想做以下事情:

每 5 秒访问一个 url,从 url 中获取 json 内容,并显示在 iphone 应用程序屏幕上。

我怎样才能使用钛移动做到这一点?

我尝试使用 onload 和 addEventListener 函数,但无济于事......

4

1 回答 1

1

我的代码只是向您展示如何获取数据,但并非每 5 秒一次。它在每个请求加载后 5 秒执行抓取 json。

function grab_json() {
    // do something with grabbing json data
    var xhr = Titanium.Network.createHTTPClient({
        onload: function(e) {
            // do something

            // when finish, grab the json data in the next 5 seconds
            setTimeout(function() {
                grab_json();
            }, 5000);
        }
    });
    xhr.open("GET", "your-url-here");
    xhr.send();
}

// execute the function when view loaded
grab_json();
于 2012-08-22T16:49:46.803 回答