2009-06-30 26 views
15

Có lẽ không có sự khác biệt, nhưng là một trong hai cách tốt hơn so với người kia (hoặc có lẽ là một bí ẩn 'ba' cách tốt hơn so với cả hai!) ...jQuery tôi nên sử dụng nhiều ajaxStart/ajaxStop xử lý


đầu tiên:

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

}); 

thứ hai:

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

Tính đến jQuery 1.8, phương pháp này .ajaxStart() nên chỉ được gắn liền với tài liệu. – ThdK

Trả lời

42

Một thực tế thú vị là ajaxStart, vv thực sự chỉ là các sự kiện jQuery. Ví dụ:

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

tương đương với:

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

Điều này có nghĩa rằng bạn cũng có thể đính kèm không gian tên để ajaxStart/ajaxStop, vv Mà cũng có nghĩa là bạn có thể làm:

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

Bạn cũng có thể làm:

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

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

Và sau đó:

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

Cool, huh?

+0

chắc chắn là! Tôi đang giả định ở đây, nhưng bạn đang nói rằng nó không quan trọng theo cách nào? – davidsleeps

+0

trình trợ giúp ajaxStart tương đương với trình trợ giúp nhấp chuột, mà chỉ ủy nhiệm để ràng buộc, vì vậy có, không quan trọng bằng cách nào. –

+7

wow, đó là Yehuda Katz! – dfens

2

Sử dụng Ajax Gọi 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>

Các vấn đề liên quan