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;
});
};
};