Ver Mensaje Individual
  #5 (permalink)  
Antiguo 20/07/2011, 17:13
leif_sk8er
 
Fecha de Ingreso: junio-2009
Mensajes: 309
Antigüedad: 14 años, 10 meses
Puntos: 5
Respuesta: ajax asincrono

Increible pero cierto! Ahi estaba el problema!!!, tengo este script para testear:
Código Javascript:
Ver original
  1. <?php /*
  2.  
  3. Author:   Luis Siquot
  4. Purpose:  Check ajax performance and errors
  5. License:  GPL
  6. site5:    Please don't drop json requests (nor delay)!!!!
  7.  
  8. */
  9.  
  10. $r = (int)$_POST['r'];
  11. $w = (int)$_POST['w'];
  12. if($r) {
  13. session_start();
  14. session_write_close();
  15.  
  16.     /*if($r < 5){
  17.     sleep($w + 15);
  18.     }else{*/
  19.      sleep(rand(1,5));    
  20.     //}
  21.    echo $_POST['r'];
  22.    die ();
  23. }  //else
  24. ?><head>
  25. <script type="text/javascript" src="jquery.js"></script>
  26. <script type="text/javascript">
  27.  
  28. var _settimer;
  29. var _timer;
  30. var _waiting;
  31.  
  32. $(function(){
  33.   clearTable();
  34.   $('#boton').bind('click', donow);
  35. })
  36.  
  37. function donow(){
  38.   var w;
  39.   var estim = 0;
  40.   _waiting = $('#total')[0].value * 1;
  41.   clearTable();
  42.   for(var r=1;r<=_waiting;r++){
  43.        w = Math.floor(Math.random()*6)+2;
  44.        estim += w;
  45.        dodebug({r:r, w:w});
  46.        
  47.        $.post("index2.php", {r:r, w:w}, function(data){
  48.        console.log(data);    
  49.        });
  50.        
  51.        /*
  52.        $.ajax({url: '<?php echo $_SERVER['SCRIPT_NAME']; ?>',
  53.                data:    {r:r, w:w},
  54.                dataType: 'json',   // 'html',
  55.                type: 'GET',
  56.                success: function(CBdata, status) {
  57.                CBdebug(CBdata);
  58.                }
  59.        });
  60.        */
  61.   }
  62.   doStat(estim);
  63.   timer(estim+10);
  64. }
  65.  
  66. function doStat(what){
  67.     $('#stat').replaceWith(
  68.        '<table border="0" id="stat"><tr><td>Request Time Sum=<th>'+what+
  69.        '<td>&nbsp;&nbsp;/2=<th>'+Math.ceil(what/2)+
  70.        '<td>&nbsp;&nbsp;/3=<th>'+Math.ceil(what/3)+
  71.        '<td>&nbsp;&nbsp;/4=<th>'+Math.ceil(what/4)+
  72.        '<td>&nbsp;&nbsp;/6=<th>'+Math.ceil(what/6)+
  73.        '<td>&nbsp;&nbsp;/8=<th>'+Math.ceil(what/8)+
  74.        '<td> &nbsp; (seconds)</table>'
  75.     );
  76. }
  77.  
  78. function timer(what){
  79.   if(what)         {_timer = 0; _settimer = what;}
  80.   if(_waiting==0)  {
  81.     $('#showTimer')[0].innerHTML = 'completed in <b>' + _timer + ' seconds</b> (aprox)';
  82.     return ;
  83.   }
  84.   if(_timer<_settimer){
  85.      $('#showTimer')[0].innerHTML = _timer;
  86.      setTimeout("timer()",1000);
  87.      _timer++;
  88.      return;
  89.   }
  90.   $('#showTimer')[0].innerHTML = '<b>don\'t wait any more!!!</b>';
  91. }
  92.  
  93.  
  94. function CBdebug(what){
  95.     _waiting--;
  96.     $('#req'+what.r)[0].innerHTML = 'x';
  97. }
  98.  
  99.  
  100. function dodebug(what){
  101.     var tt = '<tr><td>' + what.r + '<td>' + what.w + '<td id=req' + what.r + '>&nbsp;'
  102.     $('#debug').append(tt);
  103. }
  104.  
  105.  
  106. function clearTable(){
  107.     $('#debug').replaceWith('<table border="1" id="debug"><tr><td>Request #<td>Wait Time<td>Done</table>');
  108. }
  109.  
  110.  
  111. </script>
  112. </head>
  113. <body>
  114. <center>
  115. <input type="button" value="start" id="boton">
  116. <input type="text" value=8 id="total" size="2"> concurrent json requests
  117. <table id="stat"><tr><td>&nbsp;</table>
  118. Elapsed Time: <span id="showTimer"></span>
  119. <table id="debug"></table>
  120. </center>
  121. </body>


Aunque está un poco modificado el script para simplificarlo rapidamente he podido comprobar lo que comentabas...

Ahora empieza la tarea dificil, ver como termino las sesiones al principio del script siempre, y iniciarlas al final del script solo un instante para sobreescribir resultados... espero que no me cueste mucho ^^

Gracias!