Solved

Javascript delay before calling function

Posted on 2008-10-27
3
740 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

864 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now