Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var xhttp = new XMLHttpRequest();
xhttp.open("GET","https://bitbloxs.herokuapp.com/boxes?api_key=thisistheapikey");
xhttp.onload = (e)=>{
console.log(xhttp.status);
console.log(xhttp.responseText);
}
xhttp.send(null);
---
for(var i=1;i<=400;i++){
const myint=i;
setTimeout(function(){
console.log("sending")
var myxhttp = new XMLHttpRequest();
myxhttp.open("POST","https://bitbloxs.herokuapp.com/change/"+myint+"/blue?api_key=thisistheapikey");
myxhttp.send();
},200);
}
---
function changeMyColour(verb, mycolor, myindex){
console.log(verb + " " + myindex + " " + mycolor);
var myxhttp = new XMLHttpRequest();
myxhttp.open(verb,"https://bitbloxs.herokuapp.com/change/"+myindex+"/"+mycolor+"?api_key=thisistheapikey");
myxhttp.send();
}
function changeAllColoursTo(mycolor, delay){
var xhttp = new XMLHttpRequest();
xhttp.open("GET","https://bitbloxs.herokuapp.com/boxes?api_key=thisistheapikey");
xhttp.onload = (e)=>{
console.log(xhttp.status);
console.log(xhttp.responseText);
var boxes = JSON.parse(xhttp.responseText);
for(var i=1;i<=400;i++){
var box=boxes["box_"+i];
var myindex = i;
if(box.color=="white"){ // set to my colour
setTimeout(changeMyColour, i*delay, "POST", mycolor, myindex);
}
if(box.color!=mycolor){ // change it
setTimeout(changeMyColour, i*delay, "PUT", mycolor, myindex);
}
}
}
xhttp.send(null);
}
changeAllColoursTo("blue", 100);
---
myColourBot = window.setInterval(function(){changeAllColoursTo("blue",100)},4000); // runs every 4 seconds
// window.clearInterval(myColourBot)
// yellow, blue, green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment