-2

I have a small notification & alert system.

I'm just trying to detect if the window's state is blur or focus and then listing these alerts and notifications. But my clearInterval method doesn't work. Here is the code;

$(document).ready(function(){
     setTimeout(function(){loadMessageNotifications()},600);
     setTimeout(function(){loadStoryNotifications()},400);

       var intervalStoryAlerts = setInterval(function(){loadStoryAlerts()},6000);
       var intervalMessageAlerts = setInterval(function(){loadMessageAlerts()},16000); 

    $(window).bind("focus", function(event){

         setTimeout(function(){loadMessageNotifications()},600);
         setTimeout(function(){loadStoryNotifications()},400);

       var intervalStoryAlerts = setInterval(function(){loadStoryAlerts()},6000);
       var intervalMessageAlerts = setInterval(function(){loadMessageAlerts()},16000); 

    }).bind("blur", function(event){

        clearInterval(intervalStoryAlerts);
        clearInterval(intervalMessageAlerts);
    });
});

The console.log() output for these clearInterval is undefined.

AD7six
  • 63,116
  • 12
  • 91
  • 123
  • hi and welcome on stackoverflow. Can you please better specify which is the question? Thanks! – Daniele B Jan 13 '13 at 22:16
  • 1
    As an aside, note that your code can be simplified by removing the anonymous functions: `setTimeout(loadMessageNotifications, 600)` rather than `setTimeout(function(){loadMessageNotifications()},600)` (and so forth). You only need the anonymous functions if you need to pass parameters to `loadMessageNotifications()` and your other functions. – nnnnnn Jan 13 '13 at 22:22
  • 1
    Please read up on `var` and variable scopes (use those as search phrases). –  Jan 13 '13 at 22:23
  • I gave a -1 for "doesn't work" (please avoid this and similar phrases); make the title / problem summary more accurate of the real error/cause - this is done, but only on the last line of the post. Consider this summary / problem description: "variable undefined in different callback" –  Jan 13 '13 at 22:28
  • Thank you for your suggestions and answers. I solve the problem with using 'window.' here is the code http://d.pr/i/i8Yp DanieleB thank you for your kindness. I made ​​a mistake about scopes. I was monitoring a undefined error when I logging clearIntervals. pst thanks your -1. That's really improve me. Moreover I read up on the scopes, thank you. – Coşku DEMİRHAN Jan 13 '13 at 23:26

2 Answers2

2

It may be because you are using the wrong (out of scope) intervalStoryAlerts and intervalMessageAlerts, so they point at new intervals. Instead drop the re-declaration in the bind for the focus. Try:

// variables defined in enclosing scope
var intervalStoryAlerts = ..;
var intervalMessageAlerts = ..;

$(window).bind("focus", function(event){

     setTimeout(function(){loadMessageNotifications()},600);
     setTimeout(function(){loadStoryNotifications()},400);

     // remove var here so that new variables are not created
     intervalStoryAlerts = setInterval(function(){loadStoryAlerts()},6000);
     intervalMessageAlerts = setInterval(function(){loadMessageAlerts()},16000); 
})
Igor
  • 33,276
  • 14
  • 79
  • 112
  • Thank you for your interest. That works fine now. Using window solves the problem. http://d.pr/i/i8Yp – Coşku DEMİRHAN Jan 13 '13 at 22:04
  • 1
    @pst The variables are already declared in an enclosing scope, it simply was declared twice. This does not create a global variable, just re-initialize an existing one. – Igor Jan 13 '13 at 22:30
1
….bind("focus", function(event){
   // […]
   var intervalStoryAlerts = setInterval(loadStoryAlerts, 6000);
   var intervalMessageAlerts = setInterval(loadMessageAlerts, 16000); 

})

Those var declarations make these two variables local to the function, and your code will not overwrite the variables in the ready-handler's scope. Therefore, the values will be lost and the intervals are not cleared. Just omit "var".

Bergi
  • 630,263
  • 148
  • 957
  • 1,375