2009-06-30 17 views
15

Forse non v'è alcuna differenza, ma in entrambi i casi è migliore rispetto agli altri (o forse un misterioso 'terzo' modo migliore di entrambi!) ...jQuery dovrei utilizzare più ajaxStart/ajaxStop gestendo


primo:

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)); 
    }); 

}); 

secondi:

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

Come di jQuery 1.8, il metodo .ajaxStart() dovrebbe essere installata solo documento. – ThdK

risposta

42

Un fatto interessante è che ajaxStart, ecc. Sono in realtà solo eventi jQuery. Per esempio:

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

è equivalente a:

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

Questo significa che si può anche collegare i namespace per ajaxStart/ajaxStop, etc. Il che significa anche che si può fare:

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

Si potrebbe anche fare:

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

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

E poi:

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

fresco, eh?

+0

è certamente! sto assumendo qui, ma stai dicendo che non importa in che modo? – davidsleeps

+0

l'helper ajaxStart è equivalente al click helper, che si limita a legare, quindi sì, non importa in alcun modo. –

+7

wow, questo è Yehuda Katz! – dfens

2

Usa Ajax chiamata This Way ....

<!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>

Problemi correlati