Forum Topic: Javascript; delay ajax call for PHP

(201 views • 12 replies)

This topic is 1 page long.

<< < > >>
Happy

citricsquid

Reply To Post Reply & Quote

Posted at: 7/20/09 04:28 PM

citricsquid DARK LEVEL 23

Sign-Up: 06/25/05

Posts: 16,432

Hi,

I have a php function that does some "work", it takes around 4 seconds to run and it's rather annoying having it run on page load because it delays page load, so I decided to have a little twiddly spinner that calls the php AFTER the page loads through AJAX. So, I created a file just with "lol" and then used it as the called file and it worked perfectly, spinner displayed and then the "lol" showed, so then I switched it to the PHP and it fails.

I'm assuming this is because the PHP doesn't return anything UNTIL it's all processed and the javascript calls instantly. So the 4 seconds that PHP takes to run is too long for the javascript to "wait". I took a look around and all the javascript sleeps don't seem to work properly.

How would I get this to work, which sleep/timeout function should I be using and how would I use it?

function getHTTPObject() {
if (typeof XMLHttpRequest != 'undefined') {
return new XMLHttpRequest();
}
try {
return new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
return new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
return false;
}

var http = getHTTPObject();

function handleHttpResponse() {

if (http.readyState == 4) {
var temp = http.responseText;
document.getElementById("div").innerHTML=temp;
}
}


function LoadFile(){
var url = "files.php";
http.open("GET", url, true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}

None

Relish

Reply To Post Reply & Quote

Posted at: 7/20/09 04:34 PM

Relish NEUTRAL LEVEL 06

Sign-Up: 01/22/08

Posts: 787


None

citricsquid

Reply To Post Reply & Quote

Posted at: 7/20/09 04:46 PM

citricsquid DARK LEVEL 23

Sign-Up: 06/25/05

Posts: 16,432

That's what I was using, but it doesn't seem to work. I just did some more testing and if I set the timeout to 5 seconds: I can click the button then if I wait >5 seconds and click it again it updates, if I click it before it does nothing. If I set the timeout to 20 seconds I can click it as much as I want but it won't update until I click after the 20 seconds AFTER the first click.

The code is the same as above, but I set it to:

function LoadPage(){
var url = "file.php";
http.open("GET", url, true);
setTimeout("http.onreadystatechange = handleHttpResponse", 20000);
http.send(null);
}

am I doing it wrong? I don't know much JS, it's scary.


None

Relish

Reply To Post Reply & Quote

Posted at: 7/20/09 04:58 PM

Relish NEUTRAL LEVEL 06

Sign-Up: 01/22/08

Posts: 787

Hmm.

It doesn't seem to be a problem with the Javascript. I put some PHP in a file, and called it on page load in another file, and it loads fine.


None

citricsquid

Reply To Post Reply & Quote

Posted at: 7/20/09 05:00 PM

citricsquid DARK LEVEL 23

Sign-Up: 06/25/05

Posts: 16,432

At 7/20/09 04:58 PM, Relish wrote: Hmm.

It doesn't seem to be a problem with the Javascript. I put some PHP in a file, and called it on page load in another file, and it loads fine.

Are you sure? I did it with plain HTML, a blank PHP file and the actual PHP file and it does the same everytime. Does it wait 20 seconds and output for you, then?


Resigned

Relish

Reply To Post Reply & Quote

Posted at: 7/20/09 05:08 PM

Relish NEUTRAL LEVEL 06

Sign-Up: 01/22/08

Posts: 787

At 7/20/09 05:00 PM, citricsquid wrote: Are you sure? I did it with plain HTML, a blank PHP file and the actual PHP file and it does the same everytime. Does it wait 20 seconds and output for you, then?

I actually tried it without the timeout. Hmm.

After it checks the readyStatus, check to make sure that the status is equal to 200. Even if that doesnt fix your problem, its still something that should be there.

Gimme a minute and ill experiment with the timeout.. Could you by any chance post any of the PHP?


None

citricsquid

Reply To Post Reply & Quote

Posted at: 7/20/09 05:13 PM

citricsquid DARK LEVEL 23

Sign-Up: 06/25/05

Posts: 16,432

The PHP just queries a database which has quite a lot of rows - think millions - and gets quite a bit of data, posting it doesn't really need to be done it's just a query. The same affect is given when using:

<?php
sleep(4);
echo "lol";
?>

So if you need the equivalent PHP just use that. I think you could be right about the status, maybe that's to do with it? Apparently 200 means completed, could 202 be a good code to use?


None

Relish

Reply To Post Reply & Quote

Posted at: 7/20/09 05:31 PM

Relish NEUTRAL LEVEL 06

Sign-Up: 01/22/08

Posts: 787

You would want to use 200, which means the response was good and everything's okay.


None

Relish

Reply To Post Reply & Quote

Posted at: 7/20/09 05:32 PM

Relish NEUTRAL LEVEL 06

Sign-Up: 01/22/08

Posts: 787

Also, I threw this together, with the PHP example you posted. It works fine.

http://24.165.104.36/Public/Ajax/

I believe that the request will run until it either times out or returns content.


Kissing

citricsquid

Reply To Post Reply & Quote

Posted at: 7/20/09 05:37 PM

citricsquid DARK LEVEL 23

Sign-Up: 06/25/05

Posts: 16,432

what the buggery, it worked perfectly. Is there any difference between that JS and mine? It works perfectly.

I love you, Relish.


None

Afro-Ninja

Reply To Post Reply & Quote

Posted at: 7/21/09 01:18 AM

Afro-Ninja EVIL LEVEL 38

Sign-Up: 03/02/02

Posts: 13,460

using timeouts or sleeps in a case like this is a bad idea because you never know exactly how long the script will take to execute

BBS Signature

Questioning

citricsquid

Reply To Post Reply & Quote

Posted at: 7/21/09 06:11 AM

citricsquid DARK LEVEL 23

Sign-Up: 06/25/05

Posts: 16,432

At 7/21/09 01:18 AM, Afro-Ninja wrote: using timeouts or sleeps in a case like this is a bad idea because you never know exactly how long the script will take to execute

how should it be done then?


None

Afro-Ninja

Reply To Post Reply & Quote

Posted at: 7/21/09 02:45 PM

Afro-Ninja EVIL LEVEL 38

Sign-Up: 03/02/02

Posts: 13,460

At 7/21/09 06:11 AM, citricsquid wrote:
At 7/21/09 01:18 AM, Afro-Ninja wrote: using timeouts or sleeps in a case like this is a bad idea because you never know exactly how long the script will take to execute
how should it be done then?

oh, well that was just in response to the original method you were trying to implement. waiting on the status return of the ajax call should be sufficient unless I've misunderstood something about what you're doing here.

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 06:51 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!