2009-06-30 13 views
15

हो सकता है कि वहाँ कोई अंतर नहीं है, लेकिन किसी भी तरह से अन्य की तुलना में बेहतर (या शायद एक रहस्यमय 'तीसरा' जिस तरह से दोनों की तुलना में बेहतर!) है का उपयोग करना चाहिए ...jQuery मैं कई ajaxStart/ajaxStop निपटने


पहली:

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

}); 

दूसरा:

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

jQuery 1.8 के रूप में, .ajaxStart() विधि केवल दस्तावेज़ के साथ संलग्न किया जाना चाहिए। – ThdK

उत्तर

42

एक दिलचस्प तथ्य यह है कि AJAXStart, आदि वास्तव में सिर्फ jQuery घटनाएं हैं। उदाहरण के लिए:

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

के बराबर है:

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

इसका मतलब यह है कि आप भी ajaxStart/ajaxStop, आदि कौन सा भी मतलब है कि आप कर सकते हैं करने के लिए नामस्थान संलग्न कर सकते हैं:

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

आप यह भी कर सकते हैं:

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

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

और फिर:

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

कूल, हुह?

+0

निश्चित रूप से है! मैं यहां मान रहा हूं, लेकिन आप कह रहे हैं कि इससे कोई फर्क नहीं पड़ता? – davidsleeps

+0

AJAXStart सहायक क्लिक क्लिक सहायक के समतुल्य है, जो केवल बाध्य करने के लिए प्रतिनिधि है, इसलिए हाँ, इससे कोई फर्क नहीं पड़ता। –

+7

वाह, ये येहुदा काट्ज़ है! – dfens

2

उपयोग अजाक्स कॉल दिस वे ....

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

संबंधित मुद्दे