Wednesday, September 28, 2011

Execute Methods after a delay in Javascript

In rare situations you might want to call methods after some time delay. In server side code(C#.NET) you can achieve this by making thread sleep for some time. But remember in this case the overall execution of the server side code stops and resumes after the specified sleep time span is over. This results in delay in overall response time to the user.

Ever wondered how you can achieve the same thing on Client Side (in javascript). Javascript provides you a very handy built-in function “setTimeout”. Using this function you can call any method with after a specified period of time.

Let’s take one example and I will show you how to make use of this function. Assume I have one method “UpdateTotals(LocationID)” in my javascript and I want to call this method after a delay of say 4 seconds.

To accomplish this I need to call the method as shown below.

setTimeout("UpdateTotal(LocationID)", 4000);

You can try out this simple example by yourself. And for more examples you can refer this useful post.

One important thing to remember here is, this delay is asynchronous. Meaning it will only delay the execution of that particular method and it will continue with execution of other javascript code. And this is what crucial in AJAX based application where you don’t want any delay in overall execution.

Wednesday, September 21, 2011

Real AJAX and Javascript

As requirement for my current project I was asked to enhance the application performance by using AJAX. For most of the developers (especially .Net developers), AJAX implementation is nothing but to use some update panels, collapsible panels and modal popups. But when I understood the requirements from my client, I was sure that we need to deliver something more than Microsoft AJAX and there is a need to dive into Real AJAX Sea.

Now a days I am spending most of my time sharpening my Javascript skills which are mandate when you want to build real AJAX applications (not merely by using UpdatePanels).

In coming days I will try to post some cool stuff from Javascript and AJAX which I come across. And I am hopeful that these post will be useful for some of the audience out there to understand and realize effectiveness of Real AJAX.


cheers,

Mirza Ateeq