Reputation: 2295
For a web app, i am trying to fetch data from internal API and show it changing every second or so on the homepage. The internal PHP sets two variables, which are read by folowwing javascript code :
$(window).load(function() {
setInterval(function() {
var stats = "";
stats += "<span class='styleForStats'>";
stats += stat1;
stats += "</span>";
$('#myBox').html(stats);
}, 1000);
});
Now, the problem with this is, i set the variable stat1 through PHP, and that is done on load time of the page. So it won't get the latest value from PHP code.
Can i somehow, make a call to internal PHP file from JS everytime within the setInterval function so as to get live values every time. I thought of AJAX calls, but wouldnt they be only for external APIs ?
Upvotes: 1
Views: 234
Reputation: 578
You should use AJAX calls for that (and no, AJAX requests aren't just a thing for external APIs). If you need to update your data so frequently, consider using some more advanced techneques for that (such as WebSockets, Long polling, SocketIO) - it would be better solution for your case. Check out this answer for some more information about these techniques.
Upvotes: 1