Solved

Javascript delay before calling function

Posted on 2008-10-27
3
748 Views
Last Modified: 2013-11-19
I have a <td> that gets updated via an ajax call, upon the return of that call, I need to run a function that updates the style based on the innerHTML value.  If I add an alert after the ajax return and before the call to the update function, it works correctly.  However without the alert, the value that is passed in is null.  This leads me to believe that I need time for the HTML document to be updated.  I have tried the code below to delay, but this does not work either.  

I have also tried putting the checkUpdate function into the setTimeout, but I can't get the arguments to pass in.

Can anyone help?

Thanks in advance.
function checkUpdateDelay(){
 
	setTimeout('delay()', 1000);
}
 
function delay(){
var a = 0;
var a = a + 1;
}
 
	//set delay of 1ms to let response load
	checkUpdateDelay();
 
	checkUpdate(trid,document.getElementById(trid).innerHTML);

Open in new window

0
Comment
Question by:cartereverett
3 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 250 total points
ID: 22817784
>>I need to run a function that updates the style based on the innerHTML value.
OK, are you doing an asynchronous call? IF yes, you need to make the call on the callback function that you should have assigned to your onreadystatechange listener. So basically what you need is similar to:
xmlobj.onreadystatechange=function(){
  if(xmlobj.readyState==4){
   //do whatever
  //make the call here INSTEAD of the delay you are referring to
  }
}
0
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 22819404
Here is the correct way to use the setTimeout () in your case --

setTimeout('ckUpd()', 3000);

function ckUpd() {
        checkUpdate(trid,document.getElementById(trid).innerHTML);
}

If you need to change any variables, you should be putting the variable as a GLOBAL, and only changing that value in the function.  Do not embed the variables in the function, as they will have no remembrance.
0
 

Author Closing Comment

by:cartereverett
ID: 31510563
Thanks for the help!
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question