1

I'm currently coding a script where javascript sends a heartbeat to the server every 10 seconds but i'm encountering the problem that i cant access the variable out of the function. The example below returns an empty string.

var heartbeat = new function () {

    this.send = function (token) {

            $.get("https://" + storage.getapi() + "/server/heartbeat.php", function (data, status) {
                var response = JSON.parse(data);

                var status = response.status;
            });

            return status;

    };

};

The other example below returns undefined since only in the $.get callback the variable returns

var heartbeat = new function () {

    this.send = function (token) {

            $.get("https://" + storage.getapi() + "/server/heartbeat.php", function (data, status) {
                var response = JSON.parse(data);

                return response.status;
            });

    };



};
SelectJB
  • 25
  • 6
  • This has been covered thousands of times on this website - can you include a reference to a popular iteration of this problem and explain what about it doesn't make sense? – Marty Aug 08 '17 at 05:04
  • *Go to the supermarket and buy a cup of coffee. Give me the coffe now.* -> you get an empty coffee... – Jonas Wilms Aug 08 '17 at 05:24

0 Answers0