javascript - How to Handle delays that made by multiple timeouts (containing ajax calls) -
consider have multiple (sometimes more 12) ajax calls calling every 2 seconds or more. data gathered through calls set ui contained elements (like progress bars). after have delay on scroll while timers working . delay natural, how can handle it?
note: calls destinations services provides data minimum spent time. point makes scroll sad, using multiple settimeout()
, setinterval()
methods. more familiar work, see below code:
function filldata(accessurl, name) { var add = accessurl; $.support.cors = true; if (add) { $.ajax({ type: 'get', url: accessurl, crossdomain: true, contenttype: 'application/json; charset=utf-8', datatype: 'json', success: function (data) { update(name, data); }, error: function (xhr, status, error) { logresponseerrors(status , error, name); } }); settimeout(function () { filldata(accessurl, name); }, interval); //consider method calls different parameters 1 time , run automatically settimeout } else { freezeframe(name); } }
used tags explains used.
any useful answer appreciated
from understand in question. have delay when you're handling ajax responses , need remove delay.
javascript single-threaded. therefore, if there function takes long time complete, dominate thread , cause ui not responding. deal this, have 2 options:
- optimize code function not take long.
- use
settimeout
break function smaller pieces. example: if function executing loop of 100 items, break execute 10 times 10 items each.
update: (based on updated question):
it seems loop never stops when use settimeout
this. should have like:
counter++; if (counter <= 12) settimeout(function () { filldata(accessurl, name); }, interval);
due timing problem between ajax , settimeout
, @ points, there lot of events (escalated) waiting in queue executed , cause performance problem. try putting settimeout
inside success
or complete
function
Comments
Post a Comment