具有相同变量名的不同外部.js文件 [英] Different external .js files with same variable names

查看:141
本文介绍了具有相同变量名的不同外部.js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个显示来自不同位置的噪声测量数据的网站.每个位置的数据都在声级计设备上捕获,然后通过基于Windows的应用程序读取.然后,应用程序将数据作为.js文件上传到Web服务器上,其中包含数组变量.该.js文件每5分钟刷新一次.
我首先创建了一个JavaScript应用程序,该应用程序显示单个测量单位的实时数据.但是现在我需要在地图上显示所有位置的数据.问题在于,每个位置上的Windows应用程序都仅在另一个位置上创建具有相同名称和相同变量的文件.我在读取正确的数据时遇到了麻烦.
这是我到目前为止所做的:

I'm making a websites that displays noise measurement data from different locations. The data for each location is captured on a sound level meter device and it is then read with a windows-based application. The application then uploads data on a web server as a .js file with an array variable in it. This .js files are refreshed every 5 minutes.
I first created a javascript application that displays live data for a single measuring unit. But now I need to display data on a map for all the locations. The problem is that the windows application on each location makes a file with the same name and same variables only on another location. I'm having some trouble with reading the correct data.
This is what I did so far:

    function removejscssfile(filename, filetype){
         var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist from
         var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
         var allsuspects=document.getElementsByTagName(targetelement)
         for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
          if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1)
           allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild()
         }
        }

    function updateData(){
    var numberOfNoiseSniffers = noiseSniffers.length-1;
    var j = 0;
    for (i=0;i<=numberOfNoiseSniffers;i++) {
        file = '../'+ noiseSniffers[i] + "/" + "CurrentMeasurement.js";
        $.include(file,function(){
            laeq[j] = currentMeas[1][1];
            lastUpdate[j] = currentMeas[0][1];
            if (j==numberOfNoiseSniffers){
                updateMarkers();
            }
            removejscssfile(file[0], "js");
            j++;
        });
    }

    t=setTimeout(function() { updateData() }, 300000);
}


    $(function (){
    map = new google.maps.Map(document.getElementById("gMap"), myOptions);
    //noiseSniffers is an array where I have save all the folder names of different measurement locations
    var numberOfNoiseSniffers = noiseSniffers.length-1;
    var j = 0;
    for (i=0;i<=numberOfNoiseSniffers;i++) {
        var file = '../'+ noiseSniffers[i] + "/" + "CurrentMeasurement.js";
        //I am using include plugin for jquery to include files because it has a callback for when a file is actually loaded
        $.include(file,function(){
            //a set of global arrays that keep the data from the loaded file and this data is then displayed in google maps markers
            laeq[j] = currentMeas[1][1];
            lastUpdate[j] = currentMeas[0][2];
            latitude[j] = systemstats[12][5];
            longitude[j] = systemstats[11][6];
            //checking to see if I am in the process of including the last file
            if (j==numberOfNoiseSniffers){
                //a function that creates google maps markers
                createMarkers();
            }
            //after that I remove the files that were just included and read
            removejscssfile(file, "js");
            j++;
        });
    }
    setTimeout(function() { updateData() }, 300000);
    });

我在此处具有删除.js文件的功能:动态删除外部JavaScript或CSS文件.
这是用于加载.js文件的jquery插件:按需包含文件.
初始加载通常有效(有时碰巧只加载一个标记或不加载任何标记.但是update函数通常会在两个位置返回相同的数据.
因此,我想知道的是,如何首先使我的代码正常工作以及如何对其进行优化.我只发布了javascript代码的主要部分,但是如果需要的话,我可以提供所有代码.感谢您的帮助.

I got the function for removing my .js file here: Dynamically removing an external JavaScript or CSS file.
And this is the jquery plugin for loading the .js file: Include File On Demand.
The initial load usually works (sometimes it happens that only one or no markers get loaded. But the update function mostly returns the same data for both locations.
So what I want to know is, how can I firstly make my code working and how to optimize it. I posted just the main parts of the javascript code, but I can provide all the code if it is needed. Thanks for any help.

推荐答案

我认为您需要某种 JSONP 之类的解决方案.

I think you need some sort of JSONP-like solution.

基本上将数据加载到服务器端,然后将其包装在方法调用中,然后再将其返回给客户端.您的回复应如下所示:

Basically load data on the server side, then wrap it in a method call before returning it to client side. Your response should look something like this:

var location_data = [1,2,3,4]
updateLocation('location_id', location_data) 

现在,您可以在客户端脚本中定义一个updateLocation()函数.现在,每次需要新数据时,您都可以使用src指向服务器端创建新的'script'标记.加载响应后,将使用正确的参数调用您的updateLocation().

Now you define an updateLocation() function in your client side script. Now, every time you need new data, you create new 'script' tag with src pointing to your server side. When the response is loaded, your updateLocation() will be invoked with correct params.

我希望这很清楚

这篇关于具有相同变量名的不同外部.js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆