It is very simple to get time taken by your jQuery function at client side(). Declare a variable and set current time in this, after your code declare another variable and set current time in this. Now get the difference between these two variable.
In this example I have taken two button and each button doing same thing. But if you see first button taking less time in comprision of second button. Because I used variable in first button.
If your function is taking long time then you must optimize your code other wise it may be hang the browser.
$('.btnFirst').click(function () {
var startTime = new Date().getTime();
// Your code start here
var panel = $('.lblTest');
panel.html('');
for (i = 0; i < 2000; ++i) {
panel.append('First Function Called<br/>');
}
// Your code end here
var endTime = new Date().getTime();
var totalTime = endTime - startTime;
alert('Execution time: ' + totalTime);
});
CODE
<!DOCTYPE html> <html> <head> <title>jQuery With Example</title> <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $('.btnFirst').click(function () { var startTime = new Date().getTime(); var panel = $('.lblTest'); panel.html(''); for (i = 0; i < 2000; ++i) { panel.append('First Function Called<br/>'); } var endTime = new Date().getTime(); var totalTime = endTime - startTime; alert('Execution time: ' + totalTime); }); $('.btnSecond').click(function () { var startTime = new Date().getTime(); $('.lblTest').html(''); for (i = 0; i < 2000; ++i) { $('.lblTest').append('Second Function Called <br/>'); } var endTime = new Date().getTime(); var totalTime = endTime - startTime; alert('Execution time: ' + totalTime); }); }); </script> </head> <body> <div class="Container"> <button type="button" class="btnFirst">First Function</button> <button type="button" class="btnSecond">Second Function</button> <p>Please click on these button to get the time taked by thsese function.</p> <div class="lblTest"></div> </div> </body> </html>
DISPLAY
Please click on these button to get the time taked by thsese function.
No comments :
Please Give Your Feedback