2009-06-30 14 views
15

Tal vez no hay ninguna diferencia, pero de cualquier manera es mejor que el otro (o tal vez una misteriosa 'tercer' mucho mejor que los dos!) ...jQuery debería usar múltiples ajaxStart/ajaxStop manejando


primero:

var startTime; 

$(document).ready(function() { 

    $("#lbl_ajaxInProgress").ajaxStart(function() { 
     // store the current date/time... 
     startTime = new Date(); 
     // update labels 
     $(this).text('Yes'); 
     $("#lbl_ajaxCallTime").text("-"); 
    }); 

    $("#lbl_ajaxInProgress").ajaxStop(function() { 
     // update labels 
     $(this).text('No'); 
     $("#lbl_ajaxCallTime").text(myFunctionThatCalculatesTime(startTime)); 
    }); 

}); 

Segundo:

var startTime; 

$(document).ready(function() { 

    $("#lbl_ajaxInProgress").ajaxStart(function() { 
     // update labels 
     $(this).text('Yes'); 
    }); 

    $("#lbl_ajaxInProgress").ajaxStop(function() { 
     // update labels 
     $(this).text('No'); 
    }); 

    $("#lbl_ajaxCallTime").ajaxStart(function() { 
     // store the current date/time... 
     startTime = new Date(); 
     // update labels 
     $(this).text("-"); 
    }); 

    $("#lbl_ajaxCallTime").ajaxStop(function() { 
     // update labels 
     $(this).text(myFunctionThatCalculatesTime(startTime)); 
    }); 

}); 
+1

A partir de jQuery 1.8, el método .ajaxStart() debe ser adjunto al documento. – ThdK

Respuesta

42

Un hecho interesante es que ajaxStart, etc. en realidad son solo eventos jQuery. Por ejemplo:

$("#lbl_ajaxInProgress").ajaxStart(function() { 
    // update labels 
    $(this).text('Yes'); 
}); 

es equivalente a:

$("#lbl_ajaxInProgress").bind("ajaxStart", function() { 
    // update labels 
    $(this).text('Yes'); 
}); 

Esto significa que también puede adjuntar a los espacios de nombres ajaxStart/ajaxStop, etc. Lo que también significa que usted puede hacer:

$("#lbl_ajaxInProgress").unbind("ajaxStart ajaxStop"); 

También podría hacer:

$("#lbl_ajaxInProgress").bind("ajaxStart.label", function() { 
    // update labels 
    $(this).text('Yes'); 
}); 

$("#lbl_ajaxInProgress").bind("ajaxStop.label", function() { 
    // update labels 
    $(this).text('No'); 
}); 

Y luego:

$("#lbl_ajaxInProgress").unbind(".label"); 

Cool, ¿eh?

+0

ciertamente lo es! Estoy asumiendo aquí, ¿pero estás diciendo que no importa en qué dirección? – davidsleeps

+0

el ajaxStart helper es equivalente al click helper, que simplemente delega para enlazar, así que sí, no importa de ninguna manera. –

+7

guau, ¡eso es Yehuda Katz! – dfens

2

Uso Ajax llamada de esta forma ....

<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
<title>Shouting Code</title> 
 
<meta charset="utf-8"> 
 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
 
<link rel="stylesheet" 
 
\t href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css"> 
 
<script 
 
\t src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
 
<script 
 
\t src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"> 
 
    </script> 
 
</head> 
 
<body> 
 
\t <button type="submit" class="btn btn-default" 
 
\t \t onclick="ajaxRequest(this);"> 
 
\t \t <i class="fa fa-refresh"></i> Ajax Call 
 
\t </button> 
 
\t <script> 
 
    function ajaxRequest(id) 
 
    { 
 
    \t // ajax request 
 
     $.ajax({ 
 
      type: 'post', 
 
      url: '/echo/html/', 
 
      data: { 
 
       html: '<p>This is echoed the response in HTML format</p>', 
 
       delay: 600 
 
      }, 
 
      dataType: 'html', 
 
      beforeSend: function() { 
 
      \t  // alert("start"); 
 
\t \t \t \t $(id).find('i').addClass('fa-spin'); 
 
\t \t \t }, 
 
      success: function(data) { 
 
       alert('Fired when the request is successfull'); 
 
      }, 
 
      complete:function(){ 
 
       // alert("stop"); 
 
\t \t \t \t $(id).find('i').removeClass('fa-spin'); 
 
\t \t \t } 
 
     }); 
 
}</script> 
 
</body> 
 
</html>