0

In an external .js file, I have defined and initialized the variable degree, and I have a function that pulls in the most current SQS call from AWS. When I run the script, it all works and degree prints fine to the console. When I did console.log(typeof degree) it was defined as a string, which is fine for my purposes. The only issue is that when loading the HTML web page, the variable prints as 'undefined'. This is my simplified JavaScript within the HTML file:

    <li id="temp"></li>
    <script src="server.js"></script>
    <script>
        var myVar = setInterval(myTimer, 1000);
        function myTimer() {
            document.getElementById("temp").innerHTML = degree;
        } 
    </script>

I have tried moving the inner JavaScript above the list item, I have moved the document.getElement line outside of the timer (all of my other code in the timer works and shows up fine). Thanks for any insight to this problem! This is the simplified JavaScript:

    var degree = 0;
    sqs.receiveMessage(params, function(err, data) {
       var jObject = JSON.stringify(data.Messages[0]);
       var parts = jObject.split(":");
       temp = String(parts[4]);
       degree = temp.substring(1,5);
    });

1 Answers1

-1

I think you try console.log(degree) with in the function itself to show if it is work or not this is first , outside the function the degree is undefined because the function itself return nothing try this code

var degree = 0;
sqs.receiveMessage(params, function(err, data) {
    var jObject = JSON.stringify(data.Messages[0]);
    var parts = jObject.split(":");
    temp = String(parts[4]);
    degree = temp.substring(1, 5);
    return degree;
});
Alexander Nied
  • 12,804
  • 4
  • 25
  • 45
Osama
  • 2,912
  • 1
  • 12
  • 15