Postponing Code Running In JavaScript

4th January 2008 - 4 minutes read time

Creating user interfaces in JavaScript can sometimes lead to a problem, especially when the interface used AJAX to load data from the server. As many actions will be event driven by the user you can find that when a user triggers lots of events all at once the browser will send out lots of AJAX requests all at once. This can easily cause bandwidth issues but can also lead to the user getting confused while they patiently await the browser to settle down and let them get on with things.

There are numerous way to get around this issue, the first being to design programs so that AJAX requests are only issued when the user has clicked on something or when they start typing

However, sometimes you can't always get around this, for example, you might want to continuously validate a input box as the user types in it. The most stable and browser friendly (not to mention user friendly) method seems to be using a combination of the setTimeOut() and clearTimeout() methods. In this way you can wait until the user has finished what they are doing before running the code that uses AJAX. The following code will print an alert box, but only after a duration of 500 milliseconds after the last onclick event on the paragraph tag.

  1. <p onclick="runIt();">Run It</p>
  2. <script type="text/javascript">
  3. var run = '';
  4.  
  5. function runIt(){
  6. if(run==''){
  7. run = setTimeout(actuallyRunIt,500);
  8. }else{
  9. clearTimeout(run);
  10. run = setTimeout(actuallyRunIt,500);
  11. };
  12. }
  13.  
  14. function actuallyRunIt(){
  15. alert('Clicked');
  16. }
  17. </script>

This code looks to see if run is equal to '', which is what the variable has been set to, but is also what the variable is set to when clearTimeout() is called. The setTimeout() function will cause the run variable to be filled with a process ID. The second parameter in the setTimeout() function is the number of milliseconds to wait for before running the function or code defined in the first parameter. The only draw back with this method is that it is not possible to send parameters along with the callback function stipulated in setTimeout(), but there is a way around this, although it is a bit ugly. When the runIt() function is run you can set the values of a number of variables defined in the global scope that will then be used within the function the is called after the delay. The following code demonstrates this.

  1. <p onclick="runIt();">Run It</p>
  2. <script type="text/javascript">
  3. var run = '';
  4. var x = 0;
  5.  
  6. function runIt(){
  7. x++;
  8. if(run==''){
  9. run = setTimeout(actuallyRunIt,500);
  10. }else{
  11. clearTimeout(run);
  12. run = setTimeout(actuallyRunIt,500);
  13. };
  14. }
  15.  
  16. function actuallyRunIt(){
  17. alert('Clicked '+x);
  18. }
  19. </script>

This code will print out the number of times that the user clicks on the p tag. Even if the user clicks on the tag lots of times it will still keep track and print the value 500 milliseconds after the last click event.

Comments

Permalink
Great work.

Kenisha (Thu, 10/23/2008 - 02:10)

Add new comment

The content of this field is kept private and will not be shown publicly.