javascript - Calling a XMLHttpRequest continuously -
at moment calling function on setinterval basis.
this function makes xmlhttprequest server update info. if there update available update image (using canvas element).
is optimum way sort of thing?
my code:
calling code:
function startfeedup() { if (tmrstartfeedup) window.clearinterval(tmrstartfeedup); tmrstartfeedup = setinterval(getnextimage, 330); } my called function:
var isprocess = false; function getnextimage() { try { if (!isprocess) { isprocess = true; var urlts = '/cloud/isnewframes.ashx?alias=' + alias + '&version=' + version + '&guidlogon=' + guidlogon; var xmlhttp = new xmlhttprequest(); xmlhttp.open("get", urlts, true); xmlhttp.timeout = 200; xmlhttp.send(); var nextts = xmlhttp.responsetext; } isprocess = false; } catch (err) { isprocess = false; document.getelementbyid("divmode2").innerhtml = err; } }
other repeating xhr call, can use html5 web sockets allows maintain connection server, whereby server push data , when needed. web sockets relatively new , aren't supported old browsers.
your xhr asyncronous should listening on onreadystatechange event instead of expecting response available directly after send() call:
xmlhttp.open("get", urlts, true); xmlhttp.timeout = 200; xmlhttp.onreadystatechange = function(){ if(xmlhttp.readystate == 4 && xmlhttp.status == 200){ console.log("received " + xmlhttp.responsetext); } } xmlhttp.send();
Comments
Post a Comment