odd behavior setting timeouts inside a function with global references in javascript

Posted by Pablo on Stack Overflow See other posts from Stack Overflow or by Pablo
Published on 2010-06-02T23:34:05Z Indexed on 2010/06/02 23:44 UTC
Read the original article Hit count: 202

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

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery