Solved

Javascript delay before calling function

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

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