-2

Can someone please help me with this. I don't know why the globally defined object 'arr' cant be recognized in the function 'go2', do I need to pass the object through the function 'go2'? Below is my example code.

    var arr = {};
    function go(){
       var id = "id";
       var val = 2;
       arr[id] = val;
    }
    function go2(name){
       console.log(name);
       // can't get this to show the name value which should be 2 defined by 'val which is defined in the 'go' function.
    }
    setInterval(function(){ 
       var id = "id";
       var name = arr[id];
       go2(name);
    }, 3000);

Edit: I was not calling the go function as others have pointed out. The code I posted here is a simplified version of what i'm currently working on. I created a fiddle here https://jsfiddle.net/david230/v99pppk1/ to show what i'm working on. The logic is the same, but my fiddle deals with google maps. I'm not able to remove a polyline based on the ID which in this case is the destination coordinates after I draw it using the drawRoute method. The drawnRouteObj is always undefined on line 29.

  • How are those functions called? – Pointy Apr 22 '17 at 21:13
  • Nothing shown ever sets any properties on `arr`. You never call go(). Confusing name for an object literal calling it `arr` which most would think to imply array which it isn't – charlietfl Apr 22 '17 at 21:14
  • My mistake, I forgot to initiate the code function in this simplified example of my code. In my code the go function is initiated in an ajax call that is called every 10 seconds. – David Shields Apr 22 '17 at 21:19
  • charlieftl I will keep this in mind for future posts. – David Shields Apr 22 '17 at 21:21
  • Please try to create an [MCVE](https://stackoverflow.com/help/mcve) from your code. Ideally, your question should be self contained. – iled Apr 23 '17 at 06:28
  • iled I read through the MVCE link you provided and to my best knowledge, that's what I've done in the fiddle that I provided. I explained the problem. I explained the result I expect. I simplified my code to be as minimal as possible. My code is complete enough to run a test. Can you please elaborate on your comment. I'm new to posting questions on stackoverflow and your feedback would be appreciated. – David Shields Apr 23 '17 at 06:56

1 Answers1

0

From the code you provided here, it looks like go never gets executed, which means that arr is an empty object so referencing arr[id] in your setInterval function will throw an error. Is that what you're experiencing?

heylookltsme
  • 124
  • 6
  • You are right. My mistake. This was a simplified version of my code logic. I forgot to initiate the go function in this code. After initiating the go function, it works. I'm still having issues with my code, but I will see if I can expand more. Thank you heylookitsme. – David Shields Apr 22 '17 at 21:18
  • Since I'm adding a value to the 'arr' object which is globally defined inside the 'go' function, am I able to use it in a different function without passing it through? I guess what i'm asking is, is the 'arr' object with the newly added value inside the 'go' function stored in cache for me to use it in other functions? – David Shields Apr 22 '17 at 21:30
  • Yes, so long as `go` is called before you try to reference any properties in the `arr` object. This might be helpful: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Variable_scope – heylookltsme Apr 22 '17 at 21:32
  • Thank you very much. I appreciate the link as well. – David Shields Apr 22 '17 at 21:36
  • No prob! Glad I could help. :) – heylookltsme Apr 22 '17 at 22:12
  • helookItsme do you think you could look at my fiddle https://jsfiddle.net/david230/v99pppk1/1/ please? the fiddle shows my code more completely. My object shows undefined on line 30. The fiddle is what I was working on when I created this question, I just tried to put the logic in more simplified terms. I apologize ahead of time for any etiquette that i'm not following on stackoverflow, i'm new to posting questions on stackoverflow. – David Shields Apr 22 '17 at 23:08