Solved

Javascript delay before calling function

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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 how to count occurrences of each item in an array.

809 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