views:

28

answers:

1

Here is the the function and the globals:

      $note_instance = Array();
      $note_count = 0;

      function create(text){
        count = $note_count++;

        time = 5000;            

        $note_instance[count] = $notifications.notify("create", text);

        setTimeout(function(){ $note_instance[count].close() }, time);
      }

The function simply opens a notification, a sets a timeout to close it in 5 seconds.

so if i call this

 create("Good Note 1");
 create("Good Note 2");
 create("Good Note 3");

Ecah note should close 5 seconds from their creation, however always and only the last note closes, in this case "Good Note 3".

Each note object has its own entry in the the $note_instance global array so the timeouts should no be overwriting themselves.

What am i missing here folks? Thanks in advance

+2  A: 

count is a global variable.

You need to change it to a local variable by adding var count inside the function.

SLaks
Thanks mate, that sure did the trick. But how come 'count' was in the global scope if it was not declared outside the function?
Pablo
`count` was never declared with the `var` operator, it was just used for the first time in the `create` function, so JavaScript assumes it has global scope.
maerics
That's how it works in JavaScript - if a variable is not declared anywhere, it's a global. That's an enormously bad idea, but it is how the language is designed to work.
Mark Bessey
Then you should accept this answer.
SLaks
LOL :) i thought i did and then i remember that for some reason the system didn't let me pick an answer, something about waiting 9 minutes. Odd! isn't it? so i forgot to come back and pick the answer.Thanks for the reminder.
Pablo