php - $.post call from a loop -
i have ul
9 li
elements. want load information these li
elements through ajax in asynch mode.
it's simple, isn't it?
i created for(i = 1; i<=9; i++)
loop, , called $.post
.
fail: i
10, because loop running more faster, $.post
. let's search $.post in loop
on net.
i found 3 solutions. 2 here, , 1 here.
all of has same effect: not works asynchronously. every time load first, second, third etc... order changing, every request wait while previous finish.
i using win 10 64bit, apache 2.4 64 bit, php 5.6 64bit. tried on debian box, effect same.
in php file, there sleep(1)
, echo 'a'
.
my first attempt:
$('.chartcontainer').each(function(index,obj) { var cnt = index + 1; $.post(getbaseurl() + 'ajax.php', {datetime: $('#chart_' + cnt).data('time'), action: 'getchartbydatetime'}, function (reponse) { $(obj).html(reponse); }); });
my second attempt:
for (var = 1; <= 9; i++) { (function (i) { var $obj = $('#chart_' + i); $.post(getbaseurl() + 'ajax.php', {datetime: $('#chart_' + i).data('time'), action: 'getchartbydatetime'}, function (reponse) { $($obj).html(reponse); }); })(i); }
my third attempt:
function loadresponse(i) { var $obj = $('#chart_' + i); $.post(getbaseurl() + 'ajax.php', {datetime: $('#chart_' + i).data('time'), action: 'getchartbydatetime'}, function (reponse) { $($obj).html(reponse); }); } $(function () { (i = 1; i<=9; i++) { loadresponse(i); } });
expected result:
every 9 li
loaded in 1 second in same time.
can lead me right solution?
edit
maybe not clear. in production, script run approx. 3 seconds. if send 1 request data back, take 9*3 = 27 seconds while response arrives. why want send 9 request, , data in 3 seconds. think why use threads.
what want data li
in "same" time. not 1 one, or in 1 request.
edit 2
ok guys, shame on me, think mislead of you. there session start in php script.
if remove everything, , echo , die after sleep. in case 5 request responding in 1 sec, other 4 later. think new thred.
from jquery manual:
by default, requests sent asynchronously (i.e. set true default). if need synchronous requests, set option false. cross-domain requests , datatype: "jsonp" requests not support synchronous operation. note synchronous requests may temporarily lock browser, disabling actions while request active.
are sure requests not sent browser? possible php script not allow multiple sessions. have tried inspecting ajax calls firebug/chrome inspector?
edit:
php writes session data file default. when request made php script starts session (session_start()), session file locked. means if web page makes numerous requests php scripts, instance, loading content via ajax, each request locking session , preventing other requests completing.
the other requests hang on session_start() until session file unlocked. bad if 1 of ajax requests relatively long-running.
possible solutions:
- do not use sessions when don't need them
close session after reading/writing necessary information:
session_write_close();
- store sessions in redis/mysql example
Comments
Post a Comment