Saturday, October 17, 2009

How to get time elapsed after loading a webpage

How to get the time elapsed time after loading a webpage, using Callback() in MS Ajax library. Here is a sample script;

[Ajax-Script]

   1: <script type="text/javascript">
   2:         // Page Load
   3:         function pageLoad() {
   4:             var loadTime = {date : new Date()};
   5:             var callback = Function.createCallback(onBtnClick, loadTime);
   6:             
   7:             $addHandler($get("Button1"), "click", callback);
   8:         }
   9:  
  10:         function onBtnClick(event, context) {
  11:             var loadTime = context.date;
  12:             var elapseTime = (new Date() - loadTime) / 1000;
  13:  
  14:             $get("Text1").value = elapseTime + " Seconds";
  15:         }
  16:     </script>

[HTML-Script]

   1: <form id="form1" runat="server">
   2:     <asp:ScriptManager ID="ScriptManager1" runat="server">
   3:     </asp:ScriptManager>
   4:     <div>
   5:         Time elapsed since PageLoad: 
   6:         <input id="Text1" type="text" />
   7:         <input id="Button1" type="button" value="Get Elapsed Time" />
   8:     </div>
   9:     </form>

Thanks.

No comments:

 
Best viewed in Internet Explorer 8.