0

i want to call jquery ajax function inside the function like this:

function mine(time){

$.get("test.php", function(data){
var mytime=data;
});           

alert(mytime);
}

it just print undefined.
my test.php file is this:

<?php echo time(); ?>

i want that value of 'mydata' is current time of php server.

2
  • Simple: Put or call every code that has to work with the response (from) inside the callback. Commented Sep 28, 2011 at 10:14
  • but i need to setinterval of that function.. .so i can't put inside.. .is there any other solution? Commented Sep 28, 2011 at 10:41

4 Answers 4

1

The callback function gets executed at a later time. Instead of document.write at, use jQuery's methods:

$.get("test.php", function(data){
   var mytime=data;

   // Replace 'body' with a selector for the element that displays time
   $('body').text(mytime);

   // Alternatively, call another function
   someOtherFunction(mytime);
});           
Sign up to request clarification or add additional context in comments.

6 Comments

i don't want to display 'mytime' in body... but i want to use this variable in another javascript function.. .
@Kartik it Well, then simply call the other JavaScript function. Updated answer with an example.
@Kartikit: You have to rethink about your flow. Make the function accept a callback which gets executed once the result is available. JavaScript makes it very easy to implement things like this.
but i have setInterval of every 5 sec. for calling the function mine(), so i have to call ajax inside my function? is there any other way to do this?
@Kartikit: Why don't you get the time of the server once and then increment that time by 5 seconds? Why do you have to connect to the server again? It is not difficult to combine setTimeout with Ajax calls, but the question is whether you have to do that at all (and it does not seems to be the case).
|
0

There may be chance of variable scope so define your variable like this

var mytime;
function mine(time){
    $.get("test.php", function(data){
        mytime=data;
    });           

    document.write(mytime);
}

2 Comments

This won't work, since the callback is executed at a later time than document.write is.
That wouldn't work, you'd have to move the document.write inside the success function.
0

You need to use synchrone mode

function validerServeur(value) { 
    var retval; 
    $.ajax( 
        url: "test.php", 
        data: { valeur: value }, 
        async: false,  // <--  This disable asynchrone mdoe
        succes: function(data) { 
            retval = data;
        } 
    ) ;
    return retval; 
} 

1 Comment

Don't make synchronous calls!
0

add the document.write(time) INSIDE the $.get 's handler like:

$.get("test.php", function(data){
var mytime=data;
document.write(mytime);
});  

1 Comment

sorry! iN that case use echo time(); as you instructed. Just move the document.write inside the function handler as shown above.

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.