简体   繁体   English

在后台加载xml文件

[英]load xml file in background

I have a page that loads an XML file with thousands of Postal Codes into memory. 我有一个页面,它将带有数千个邮政编码的XML文件加载到内存中。 Once the xml is loaded, a textbox and search button are displayed and the user can enter a postal code (zip code) and click search and some results will display. 加载xml后,将显示一个文本框和搜索按钮,用户可以输入邮政编码(邮政编码)并单击“搜索”,然后将显示一些结果。 The problem is, the initial load takes a while and the page says "loading..." for 10-15 seconds before the textbox and search button appear. 问题是,初始加载需要一段时间,并且在显示文本框和搜索按钮之前,页面会显示“正在加载...” 10-15秒。 I need to make the search box/button appear faster/immediately, even if it means an extra bit of time on the searches. 我需要使搜索框/按钮显示得更快/立即,即使这意味着搜索需要花费额外的时间。 I admit, I'm more of a .net guy and don't know javascript/ajax real well. 我承认,我更像是.net的家伙,并且不太了解javascript / ajax。 Here's the function that loads the xml. 这是加载xml的函数。 Can anyone help? 有人可以帮忙吗?

function importXML() {

    var xmlhttp;
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else { // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {

            var i,j;
            var xmlDoc=xmlhttp.responseXML;

            for (i=0; i<arrServiceProviders.length;i++) {
                var regionList = xmlDoc.getElementsByTagName("region");
                var postalCodeList = regionList[i].getElementsByTagName("postalcode");

                for (j=0;j<postalCodeList.length;j++) {

                    arrServiceProviders[i][j]=postalCodeList[j].childNodes[0].nodeValue;
                }
            // debug time:
            //alert(arrServiceProviders[i]);
            }
            var searchForm = document.getElementById("search-wrapper");
            var loadingPlaceholder = document.getElementById("loading");
            loadingPlaceholder.className = "hidden";
            searchForm.className = "";
        }
    };

    xmlhttp.open("GET","/agency-postal-codes.xml",true);
    xmlhttp.send();
}

You could use a semaphore flag to keep track of the request while it's loading and show the form right away, then stop the search from performing until the ajax request return by polling the status of the semaphore continuosly. 您可以使用信号量标记来跟踪请求的加载过程,并立即显示表单,然后通过连续轮询信号量的状态来停止执行搜索,直到ajax请求返回。 Not much elegant, but should do its job. 不太优雅,但应该做到这一点。

For example: 例如:

var  loaded = false;

function importXML() {

    var xmlhttp;
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else { // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {

            var i,j;
            var xmlDoc=xmlhttp.responseXML;

            for (i=0; i<arrServiceProviders.length;i++) {
                var regionList = xmlDoc.getElementsByTagName("region");
                var postalCodeList = regionList[i].getElementsByTagName("postalcode");

                for (j=0;j<postalCodeList.length;j++) {

                    arrServiceProviders[i][j]=postalCodeList[j].childNodes[0].nodeValue;
                }
            // debug time:
            //alert(arrServiceProviders[i]);
            }
            loaded = true;
        }
    };

    xmlhttp.open("GET","/agency-postal-codes.xml",true);
    xmlhttp.send();
}

function doSearch() {
    console.log("about to search");
    var val = document.getElementById("txt").value;
    var res = document.getElementById("result");

    res.style.display = "block";
    if (loaded) {
        // search logic goes here
        res.innerHTML = "found";
    } else {
        res.innerHTML = "Still loading...";
        setTimeout(doSearch, 1000);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM