setInterval不适用于Ajax调用(setInterval not working for ajax call)

我有一个getJson调用web服务,并正常工作,现在我试图每10秒发出一次请求。 使用带回调函数的setInterval来弹出警报。 我无法让它工作。 代码如下:

function ajxCall(){ $.getJSON('http://api.tubeupdates.com/?method=get.status&lines=all&return=name,status,messages,status_starts&jsonp=?', function (result){ $.each(result.response.lines, function(i, item){ $('#status').append("<p>"+item.name + " - " + item.status + " <br><b>" +item.messages + "</b> " + item.status_starts + "</p>"); }); }); } setInterval(ajxCall(), (10 * 1000), function(){ alert('called!') });

我究竟做错了什么?

提前致谢,

毛罗

I have a getJson call to a webservice and works fine, Now I'm trying to make the request every 10 sec. using setInterval with a callback function to fire an alert pop up. I can't make it work. Here's the code:

function ajxCall(){ $.getJSON('http://api.tubeupdates.com/?method=get.status&lines=all&return=name,status,messages,status_starts&jsonp=?', function (result){ $.each(result.response.lines, function(i, item){ $('#status').append("<p>"+item.name + " - " + item.status + " <br><b>" +item.messages + "</b> " + item.status_starts + "</p>"); }); }); } setInterval(ajxCall(), (10 * 1000), function(){ alert('called!') });

What am I doing wrong?

Thanks in advance,

Mauro

最满意答案

setInterval(function() { ajxCall(); }, 10000);

试试看

setInterval(function() { ajxCall(); }, 10000);

Try that

setInterval不适用于Ajax调用(setInterval not working for ajax call)

我有一个getJson调用web服务,并正常工作,现在我试图每10秒发出一次请求。 使用带回调函数的setInterval来弹出警报。 我无法让它工作。 代码如下:

function ajxCall(){ $.getJSON('http://api.tubeupdates.com/?method=get.status&lines=all&return=name,status,messages,status_starts&jsonp=?', function (result){ $.each(result.response.lines, function(i, item){ $('#status').append("<p>"+item.name + " - " + item.status + " <br><b>" +item.messages + "</b> " + item.status_starts + "</p>"); }); }); } setInterval(ajxCall(), (10 * 1000), function(){ alert('called!') });

我究竟做错了什么?

提前致谢,

毛罗

I have a getJson call to a webservice and works fine, Now I'm trying to make the request every 10 sec. using setInterval with a callback function to fire an alert pop up. I can't make it work. Here's the code:

function ajxCall(){ $.getJSON('http://api.tubeupdates.com/?method=get.status&lines=all&return=name,status,messages,status_starts&jsonp=?', function (result){ $.each(result.response.lines, function(i, item){ $('#status').append("<p>"+item.name + " - " + item.status + " <br><b>" +item.messages + "</b> " + item.status_starts + "</p>"); }); }); } setInterval(ajxCall(), (10 * 1000), function(){ alert('called!') });

What am I doing wrong?

Thanks in advance,

Mauro

最满意答案

setInterval(function() { ajxCall(); }, 10000);

试试看

setInterval(function() { ajxCall(); }, 10000);

Try that