最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting variable from success function ajax - Stack Overflow

programmeradmin8浏览0评论

I have a variable in the success part of ajax and I want to reuse it in another function (which executed every 3 seconds), I tried to declare it global, but it does not work; Tdata is not known. I know that $.ajax is an asynchronous function and I saw some posts similar to mine but it did not help me.

Help me please. Thank you.

This is a part of my code:

<script language='Javascript'> 
var Tdata;


$.ajax({
            method : "GET",
            url: "load-data.php",
            success : function(data){
                Tdata=jQuery.parseJSON(data);
                          ////
            }

});

window.setInterval(function() { 

    $(window).load(function() {

                $.each(Tdata, function(variable) {  
                /////////////
                });

    }); 

}, 3000);


</script>

I have a variable in the success part of ajax and I want to reuse it in another function (which executed every 3 seconds), I tried to declare it global, but it does not work; Tdata is not known. I know that $.ajax is an asynchronous function and I saw some posts similar to mine but it did not help me.

Help me please. Thank you.

This is a part of my code:

<script language='Javascript'> 
var Tdata;


$.ajax({
            method : "GET",
            url: "load-data.php",
            success : function(data){
                Tdata=jQuery.parseJSON(data);
                          ////
            }

});

window.setInterval(function() { 

    $(window).load(function() {

                $.each(Tdata, function(variable) {  
                /////////////
                });

    }); 

}, 3000);


</script>
Share Improve this question edited Sep 27, 2012 at 14:46 Mils asked Sep 27, 2012 at 13:33 MilsMils 1,5083 gold badges20 silver badges44 bronze badges 2
  • what is the problem with current code? do you get Tdata same every time? or is there some other problem? – Ankur Commented Sep 27, 2012 at 13:37
  • No I can't, Tdata is not known. – Mils Commented Sep 27, 2012 at 13:38
Add a ment  | 

4 Answers 4

Reset to default 3

Why not wait until the AJAX request has successfully returned data before starting your interval? Since any executions of the interval's function aren't going to do anything (due to no data) before that point waiting isn't going to change the way the page would function in any way.

$.ajax({
    method: "GET",
    url: "load-data.php",
    dataType: "json"
    success: function(data) {
        var Tdata = data;
        // do some more stuff with the response of the AJAX request
        var interval = setInterval(function() {
            $.each(Tdata, function(variable) {  
                // do something with variable
            });
        }, 3000);
    }
});

Note that I've removed the binding of the load event to the window every time the interval runs because doing so really doesn't seem to make any sense. I've also added a dataType property with a value of json to the options object passed to $.ajax() so you don't have to parse the response as JSON yourself.

try this,

<script language='Javascript'> 
var Tdata;


$.ajax({
            method : "GET",
            url: "load-data.php",
            success : function(data){
                Tdata=jQuery.parseJSON(data);
                          ////
            }

});

 $(window).load(function() {
window.setInterval(function() { 



                $.each(Tdata, function(variable) {  
                /////////////
                });



}, 3000);
 }); 

</script>

Function that uses the variable from AJAX call should be called from inside AJAX success, like this:

$.ajax({
        method : "GET",
        url: "load-data.php",
        success : function(data){
            Tdata=jQuery.parseJSON(data);
            myFunction();
        }
});

function myFunction(){
        var interval = setInterval(function() { 
                $.each(Tdata, function(variable) {  
                /////////////
                });
        }, 3000);
}

is callback tdataAjax function ajax success method run; @param parseJSON

var tdataAjax = function(callback) {

    $.ajax({
        method : "GET",
        url: "load-data.php",
        success : function(data){

            var Tdata=jQuery.parseJSON(data);

            setInterval(function() { 

                callback(Tdata);
            }, 3000);

       }

    });
};

is Callback function @param data in tdataAjax function

tdataAjax(function(data) {

        $.each(data, function(variable) { 

            // code
        });
});

tdataAjax ++ :)

 tdataAjax(function(data) {

        $.each(data, function(variable) { 

            // cla bla
        });
});
发布评论

评论列表(0)

  1. 暂无评论