Thanks for all the code snippets in the past - but still a bit stuck.
Trying to implement 'simple' javascript to query server side php and read and right number aims to function as timer.
Php pages - work as they should- file reads and file writes successfully.
On javascript side can read file and can write it - but not successfully together.
Presume is issue with AJax.request running for both? Seperated into different functions to try to aid but not.
Surely there is a simple way to read AND then write (via server php files - which are working) via javascript?
function getfromfile() {
var ajaxRequest;
// The variable that makes Ajax possible!
try {
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch(e) {
// Internet Explorer Browsers
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function() {
if (ajaxRequest.readyState == 4) {
document.getElementById('maxtime').innerHTML = ajaxRequest.responseText;
alert(ajaxRequest.responseText);
}
}
ajaxRequest.open("GET", "getmaxtime.php", true);
ajaxRequest.send(null);
return document.getElementById('maxtime').innerHTML;
}
function timer() {
// document.getElementById('maxtime').innerHTML = i;
document.getElementById('maxtime').innerHTML = getfromfile();
setTimeout('timer()', 1000);
//this will make loop this function for every one second
i = document.getElementById('maxtime').innerHTML;
i--;
document.getElementById('maxtime').innerHTML = i;
writetofile(i);
}
function writetofile(i) {
// Now write data back to file.txt
var ajaxRequest;
// The variable that makes Ajax possible!
// try{
// Opera 8.0+, Firefox, Safari
// ajaxRequest = new XMLHttpRequest();
// } catch (e){
// Internet Explorer Browsers
// try{
// ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
// } catch (e) {
// try{
// ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
// } catch (e){
// // Something went wrong
// alert("Your browser broke!");
// return false;
// }
// }
// }
// Create a function that will receive data sent from the server
// ajaxRequest.onreadystatechange = function(){
// if(ajaxRequest.readyState == 4){
// document.getElementById('maxtime').innerHTML = ajaxRequest.responseText;
// }
// }
alert("writemaxtime.php?time=" + i);
ajaxRequest.open("GET", "http://localhost/control/writemaxtime.php?time=" + i, true);
ajaxRequest.send(null);
}
Thanks for the comments: Yes the aim is to read, display countdown and periodically write network file holding timer information a set period of time- likely minutes - currently a second in this code. Both php files (server located) writemaxtime.php?time= work as does getmaxtime.php - giving resulting number only.
The html element is a div id element to display timer/countdown = ID 'maxtime' The issue is reading them into java to display and for javascript to manage countdown - hence the need to save.
I think I understand the asynchronous part - so the result may be blank for a period ? Obviously I can get the initial result from php - however that would be static and it needs to countdown.