我有一个我希望每5秒运行一次的JavaScript。它显示我的Raspberry Pi上GPIO引脚的状态。但是现在它不会更新,如果引脚从我正在运行的另一个脚本中恢复状态。我试图放入var myVar = setInterval(function(){ change_pin() }, 1000);
,但它对我没有帮助..
代码看起来像这样;
//TheFreeElectron 2015, http://www.instructables.com/member/TheFreeElectron/
//JavaScript, uses pictures as buttons, sends and receives values to/from the Rpi
//These are all the 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 an array for easy access later
var Buttons = [ button_0, button_1, button_2, button_3, button_4, button_5, button_6, button_7];
//This function is asking for gpio.php, receiving datas and updating the index.php pictures
function change_pin ( pic ) {
var data = 0;
//send the pic number to gpio.php for changes
//this is the 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 the 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;
}