javascript - Calling Ajax periodically -
i calling java script function view page (mvc razor) initiates ajax call. want periodically re-occur. reason going in recursive loop
function getdata(url) { $.ajax({ url: url, type: "get", contenttype: "application/json; charset=utf-8", success: onsuccess, error: onerror, complete: function(xhr, status) { settimeout(getdata(url), 5000); } }); } function onsuccess(data, status, xhr) { (var key in data) { if (data.hasownproperty(key)) { var id = data[key].id; var value = data[key].value; document.getelementbyid(id).innerhtml = value; } } }
on html page side call function on page load
<script type="text/javascript"> getdata("someurl"); </script>
any idea why going recursive when setting timeout in complete callback.
settimeout(getdata(url), 5000);
calls getdata
function , indeed goes infinite loop.
instead want pass function first argument:
settimeout(function() { getdata(url); }, 5000);
Comments
Post a Comment