Sunday, November 17, 2019

How to send api requests in real time in JS?

With this XHR request it would only send request once, if this is done with setTimeout it can repeatedly send request but how to do that in real time? Like on some websites we can see real time bitcoin prices. I read about EventSource but can't understand how it's used.

var xhr = new XMLHttpRequest() xhr.onload = function(){ if(this.status == 200){ document.write(this.responseText)' } } xhr.open("GET","https://api.coindesk.com/v1/bpi/currentprice/USD.json",true); xhr.send(); 

No comments:

Post a Comment