0

I have found a problem with use JQuery timeout, i have a page with script, it's set a timeout and waiting response from server. The problem is when timeout have to perform, in short words, it "no action". I think that script not interrupt the connection with server and wait for server response, because in server side the connection is on for 10 sec (but timeout is set 5 sec). I see this problem when server work in different domain name then client, whereas when the server and client work in local this problem there isn't.

Have you a idea because this error happened or how to close the Script connection?

// client 

$.ajax({

    type: "GET",

    url: "some.php",

    data: "name=John&location=Boston",

    timeout: 5000,

    success: function(msg){

        alert( "Data Saved: " + msg );

    },
    error: function(request, errorType, errorThrown){

        alert("opppsssss .... ");
    }
});

and the server code:

//some.php

< ?

//simulate long task

sleep(10); //sleep 10 seconds

//send response

echo "some test data";

? >

Best Regards

Domenico

1 Answer 1

3

You're doing it back-to-front by the looks of it. You've set the timeout to 5000 milliseconds (5 seconds) so if the server-side code takes longer to respond than 5 seconds it will timeout. You're doing sleep(10); which sleeps for 10 seconds, which is twice as long as the specified timeout. The way to fix this would be to do the following:

// client 
$.ajax({
    type: "GET",
    url: "some.php",
    data: "name=John&location=Boston",
    timeout: 10000,

    success: function(msg){
        alert( "Data Saved: " + msg );
    },
    error: function(request, errorType, errorThrown){
        alert("opppsssss .... ");
    }
});

And the server code:

//some.php
< ?
    //simulate long task
    sleep(5); // sleep 5 seconds!
    //send response
    echo "some test data";
? >

Hopefully that helps.

Sign up to request clarification or add additional context in comments.

2 Comments

The answer here puzzles me and does not answer my question at all. Shouldn't it be the other way around (like the OP's post)? The point is that client side will time out and do something, and in order to simulate the time out behavior, server has to sleep longer than the actual time out set on the client side.
No. When the request goes to the server, the server sleeps for 10 seconds. The client will timeout in 5 seconds (5000 milliseconds). The point is that he wants the request to wait before the server carries out some action, but in this case the request will just be thrown away because the server didn't respond in a timely manner, meaning nothing will happen at all. Crikey, this post is over three years old too! Waking up the dead? ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.