-1

Hi guys this script gets the data from my 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>

But I am not able to read the value of data_val on my other script:

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

Can someone help my out, please

1 Answer 1

0

You could use the same approach that you are using to store the value that you get from the server in the local HTML to retrieve the value in the "other script".

//from the "other script"
var level = document.getElementById("input3").innerHTML;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your response. Yes I am getting the response, in this case data_val = 150. What I want is to used that value of the variable outside of the IF. Example var x = data_val + 100; thanks
I updated my answer. Since you are already storing your value in the local HTML you can just use the same approach to pull that value out in your "other script". But if you want a better understanding of JavaScript global variables maybe this will help: stackoverflow.com/questions/11938961/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.