weixin_33712987 2014-04-05 05:43 采纳率: 0%
浏览 21

Javascript Aqax文件查询

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥65 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?