[jQuery] Easy typewriter plugin, need some help with scope
Here's my code. The plugin should take the text from a container,
erase the container, then print out the text one character at a time
in a typewriter fashion. The problem is that setInterval() seems to
think that addText() is undefined, because setInterval() runs at the
'window' scope level. I had this function running fine as a normal
program, but I'm unclear on how to convert it over to the plugin
format and solve the scope issue. thanks in advance.
jQuery.fn.typewriter = function(speed) {
return this.each(function(){
var contents = jQuery(this).html();
var count = 1;
if(contents){
jQuery(this).html("");
var interval = setInterval("addText()", speed);
}
function addText(){
jQuery(this).html(contents.substr(0, count));
count++;
if(count > contents.length){
clearInterval(interval);
}
}
});
};