JavaScript routine for calling a function every x seconds -
i have javascript want run every 5 seconds. display status of gpio pins on raspberry pi. right don't update if pin chages status script i'm running. tried put in var myvar = setinterval(function(){ change_pin() }, 1000);
don't me..
the code looking this;
//thefreeelectron 2015, http://www.instructables.com/member/thefreeelectron/ //javascript, uses pictures buttons, sends , receives values to/from rpi //these buttons var button_0 = document.getelementbyid("button_0"); var button_1 = document.getelementbyid("button_1"); var button_2 = document.getelementbyid("button_2"); var button_3 = document.getelementbyid("button_3"); var button_4 = document.getelementbyid("button_4"); var button_5 = document.getelementbyid("button_5"); var button_6 = document.getelementbyid("button_6"); var button_7 = document.getelementbyid("button_7"); //create array easy access later var buttons = [ button_0, button_1, button_2, button_3, button_4, button_5, button_6, button_7]; //this function asking gpio.php, receiving datas , updating index.php pictures function change_pin ( pic ) { var data = 0; //send pic number gpio.php changes //this http request var request = new xmlhttprequest(); request.open( "get" , "gpio.php?pic=" + pic, true); request.send(null); //receiving informations request.onreadystatechange = function () { if (request.readystate == 4 && request.status == 200) { data = request.responsetext; //update index pic if ( !(data.localecompare("0")) ){ buttons[pic].src = "data/img/red/red_"+pic+".jpg"; } else if ( !(data.localecompare("1")) ) { buttons[pic].src = "data/img/green/green_"+pic+".jpg"; } else if ( !(data.localecompare("fail"))) { alert ("something went wrong!" ); return ("fail"); } else { alert ("something went wrong!" ); return ("fail"); } } //test if fail else if (request.readystate == 4 && request.status == 500) { alert ("server error"); return ("fail"); } //else else if (request.readystate == 4 && request.status != 200 && request.status != 500 ) { alert ("something went wrong!"); return ("fail"); } } return 0; }
Comments
Post a Comment