开发者

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

Here is the the function and the globals:

      $note_instance开发者_StackOverflow = 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


count is a global variable.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜