【问题标题】:GET data from arduino从 arduino 获取数据
【发布时间】:2015-03-26 18:21:12
【问题描述】:

大家好,这个脚本从我的 arduino 获取数据。

<script>                        
    var nivel = 0;
    var data_val = 0;                                 

    function GetArduinoInputs()
    {
        nocache = "&nocache=" + Math.random() * 1000000;
        var request = new XMLHttpRequest();
        request.onreadystatechange = function()
        {
            if (this.readyState == 4) {
                if (this.status == 200) {
                    if (this.responseXML != null) {
                        document.getElementById("input3").innerHTML = this.responseXML.getElementsByTagName('analog')[0].childNodes[0].nodeValue;
                        data_val = this.responseXML.getElementsByTagName('analog')[0].childNodes[0].nodeValue;
                    }
                }
            }
        }
        request.open("GET", "ajax_inputs" + nocache, true);
        request.send(null);
        setTimeout('GetArduinoInputs()', 200);
    }

</script>

但我无法在我的其他脚本上读取 data_val 的值:

<script>
    var level = data_val;
</script>

有人可以帮帮我吗

【问题讨论】:

    标签: javascript ajax


    【解决方案1】:

    您可以使用与从服务器获取的值存储在本地 HTML 中相同的方法来检索“其他脚本”中的值。

    //from the "other script"
    var level = document.getElementById("input3").innerHTML;
    

    【讨论】:

    • 感谢您的回复。是的,我得到了响应,在这种情况下 data_val = 150。我想要的是在 IF 之外使用该变量的值。示例 var x = data_val + 100;谢谢
    • 我更新了我的答案。由于您已经将值存储在本地 HTML 中,您可以使用相同的方法在“其他脚本”中提取该值。但如果你想更好地理解 JavaScript 全局变量,也许这会有所帮助:stackoverflow.com/questions/11938961/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多