Here you can do this without global variables.
And, I think you want to use setTimeout, not setInterval, because you want the timer to run only once for each message displayed.
I also reordered the initial operations on the message object, so the data and class are set before you show it, and saved the jQuery object once in a local variable, rather than starting the selector again and again.
function message(kind, text){
var m = $('#message');
m.text(text);
m.attr('class', kind);
m.show();
var timer = m.data("timer");
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(function() {
m.fadeOut(2000);
m.data("timer", null);
},10000);
m.data("timer", timer);
}
source
share