Javascript for loop different order -
can tell me why different order everytime code loops? i'm new javscript , know code isn't neat.
<html> <body> <div id="status"> <br> </div> <script> loopthis() function loopthis(){ document.getelementbyid("status").innerhtml = "status - name: <br>" var userlist = ["amd","danielfromsl","skyrimfus","tyler1","andymilonakis","omgchad","russ_money","h3h3productions"] var i, len; (i = 0, len = userlist.length; < len; i++){ usercheck = userlist[i] var xhttp = new xmlhttprequest(); xhttp.onreadystatechange = function() { if (this.readystate == 4 && this.status == 200) { jsondoc = this.responsetext; last = jsondoc.split('"mature":') last = last[1] last = last.split(",") isonline = last[0] streamtitle = last[1].split('"status":') streamtitle = streamtitle[1] realuser = last[3].split('"display_name":') realuser = realuser[1] realuser = realuser.split('"') realuser = realuser[1] url = last[15].split('"url":') url = url[1] url = url.split('"') url = url[1] if (isonline == "true") { //alert(realuser+" streaming! title:"+streamtitle) document.getelementbyid("status").innerhtml += "<font color='0x00ff00'>online</font> - "+"<a href='"+url+"'>"+realuser+"</a><br>" } else { //alert(realuser+" offline!") document.getelementbyid("status").innerhtml += "<font color='red'>offline</font> - "+realuser+"<br>" } } }; xhttp.open("get", "https://api.twitch.tv/kraken/channels/"+usercheck+"?client_id=sky", true); xhttp.send(); } } setinterval(loopthis,5000); </script> </body> </html>
you use loop loop through list of users in predefined order. inside loop make asynchronous xmlhttprequest , give function run when receive response request. function have attached xhttp.onreadystatechange event run when response received.
the responses can received in different order everytime script run , that's why different xhttp.onreadystatechange events fire in different order.
Comments
Post a Comment