Solved

Javascript delay before calling function

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

762 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

22 Experts available now in Live!

Get 1:1 Help Now