I have several setTimeout statements and after I run my code through the closure compiler, they don’t seem to work.
I have 3 kinds of these statements:
1) Call a function:
setTimeout("MyFunctionName()", 3000);
2) jQuery
setTimeout("$('#MyDiv').find('.MyClass').addClass('TheNewClass');", 1000);
3) Global varible setting
setTimeout("MyGlobalVar = 2;", 2000);
What’s the best way to optimize this kind of code?
Thanks for your suggestions.
The closure compiler renames your functions and variables, thats why your code breaks. As a solution use an anonymous function like the following, so those calls get renamed, too.
1)
or (as mentioned by jfriend00) as long as your function has no arguments:
2)
3)
As for (2) I’m not sure, whether that’s the only reason your code breaks.