0

Tried to write a function which reads data from a text file. The function is working fine but I want to read its data in other function. Could some one help me telling how to return the value of y to read() function

<!DOCTYPE html>
<html>

    <head>
        <script charset="utf-8" src="jquery-2.0.3.min.js"></script>
        <script type="text/javascript">
            function read() {
                $.get("version.txt?_ts=" + new Date().getTime(), function (data) {
                    var y1 = parseInt(data[0]);
                    var y2 = parseInt(data[1]);
                    var y = (y1 * 10) + y2;
                    return y;
                });
            }

            function drr() {
                var d = read();
                document.write(d);
            }
        </script>

        <body onload=read()>not working</body>

</html>
Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
Krish
  • 45
  • 1
  • 8
  • 1
    possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Arun P Johny Dec 11 '13 at 08:33
  • Please read [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Adam Merrifield Dec 11 '13 at 08:34
  • Simply put you can't return value from an ajax request... you need to use a callback... read the above link to see how it is done – Arun P Johny Dec 11 '13 at 08:34
  • possible duplicate of [How to return AJAX response Text?](http://stackoverflow.com/questions/1225667/how-to-return-ajax-response-text) – Quentin Dec 11 '13 at 08:36

4 Answers4

1

In your code you are using AJAX trough JQuery:

$.get(url, callback) 

The $get method hits the url and passes the result to the callback method, it does not return it. The callback does not get invoked immediately, due to AJAX being asynchronous, instead something like this happens:

  1. $.get is called
  2. the read method continues (exits in your case, as there is no other code)
  3. the $.get receives a response from the server
  4. the callback function gets called, if the server succeeded with getting the data

In the above sequence, the second step may be swapped with any of the steps after it. Still, this sequence is to show that there is no relationship between when the read method exits and when the processing of the data occurs. So, if you need to do something with the result (show it on the page) then you should do this inside the callback method.

Here is an edited example with your code:

function read() {

    $.get("version.txt?_ts=" + new Date().getTime(), function(data) {

        var y1 = parseInt(data[0]);
        var y2 = parseInt(data[1]);
        var y = (y1*10)+y2;

        document.write(y);
    });
}

In addition to the asynchronous nature of AJAX, there is another problem with your code. In your read method, you expect to return a value, but actually it does not return anything. This renders the drr function to be invalid.

Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
0

do like this:

function read()
    {

    $.get("version.txt?_ts=" + new Date().getTime(), function(data) {


    var y1= parseInt(data[0]);
    var y2= parseInt(data[1]);
    var y=  (y1*10)+y2;

     document.write(y); /* this is the change, process the result when it's received here */

      });

    }

function drr()
    {
     read();
    }

also change html to a more proper syntax

<body unload="read();">

generally, when you have $.ajax() call, it is asynchronous - request is made and the success handler is executed, when request is processed successfully.

so if you use return value of the ajax() call, it's not the response received from the server, but rather a request object.

jancha
  • 4,916
  • 1
  • 24
  • 39
0

$.get is async function, there's no return for read function, you need use callback:

function read(callback) {

    $.get("version.txt?_ts=" + new Date().getTime(), function(data) {

        var y1 = parseInt(data[0]);
        var y2 = parseInt(data[1]);
        var y = (y1 * 10) + y2;

        callback(y);
    });

}

function drr() {

    read(function(txt) {

        document.write(txt);
    });

}
Andrew
  • 5,290
  • 1
  • 19
  • 22
  • Is there any way to get the txt value out to the drr() function? I have to use that value to draw a graph – Krish Dec 11 '13 at 09:03
  • You can assign the txt to some global variable if you like, but you can not use this until it has been set a value, right? so to draw the graph, just call the draw function instead of document.write(txt); – Andrew Dec 11 '13 at 09:14
  • Actually to draw my graph I thought of Updating the value by calling that function inside the loop. so if there is possibility to call the function regularly and get the new values I can draw the graph. but with this I am confused – Krish Dec 11 '13 at 09:21
0

Try this.

 function read() {
            var result=0;
                $.get("version.txt?_ts=" + new Date().getTime(), function (data) {
                    var y1 = parseInt(data[0]);
                    var y2 = parseInt(data[1]);
                    var y = (y1 * 10) + y2;
                    result=y;
                });
             return result;
            }

            function drr() {
                var d = read();
                document.write(d);
            }
Priyank
  • 1,353
  • 9
  • 13
  • result value is not updating. Its printing 0 – Krish Dec 11 '13 at 09:30
  • use $.ajax with async:false and cache:false option. Since get is asynchronous by default so it is returning before the success function is called. – Priyank Dec 11 '13 at 09:32