2017-04-10 175 views
-2

我使用Jquery-ajax调用从api获取信息并将其存储。我遇到的问题如下:jquery ajax请求在每个请求上呈指数级增长

当第一个电话打出电话时,一切似乎都正常。当第二次呼叫(1分钟后)再次一次呼叫发生但第三次呼叫2次sendData函数被调用时,第4次调用4次sendData函数。

请告诉我我要去哪里错。提前感谢。

$(document).ready(function() { 
    $(function ajaxCall() { 
    $.ajax({ 
     url: "https://blockchain.info/ticker", 
     method: "GET", 
     dataType: "json", 
     crossDomain: true, 
     processData: true, 
     async: false, 
     success: function(resp) { 
     if (resp != null) { 
      alert(resp); 
      var myJSON = JSON.stringify(resp); 
      alert(myJSON); 
      sendData(myJSON); 

      setInterval(ajaxCall, 1000 * 60); 
     } else { 
      alert("something went wrong"); 
     } 
     }  
    }); 
    }); 
+6

百尺竿头叫你做的时间间隔.....你的意思是使用'setTimeout'? – epascarello

+2

请删除'async:false'。这是非常糟糕的做法。你也可以在每分钟后删除包含'ajaxCall()'函数 –

+0

的jQuery对象,我再次想要这些数据,这就是为什么我已经使用setInterval –

回答

-1

我建议你移动调用方法阿贾克斯的完整方法:

$(document).ready(function() { 
    $(function ajaxCall() { 
     $.ajax({ 
      url: "https://blockchain.info/ticker", 
      method: "GET", 
      dataType: "json", 
      crossDomain: true, 
      processData: true, 
      success: function (resp) { 
       if (resp !== null) { 
        alert(resp); 
        var myJSON = JSON.stringify(resp); 
        alert(myJSON); 
        sendData(myJSON); 


       } else { 
        alert("something went wrong"); 
       } 
      }, 
      complete: function() { 
       setInterval(ajaxCall, 1000 * 60); 
      } 
     }); 
    }) 
}); 
+0

这将是伟大的downvoter解释他的意见 –