The code:
setInterval("doSomething()", 2000);
function doSomething(){alert('hi')}
demo: http://jsfiddle.net/PRff7/
I’ve been reading about this and I just can’t get the example to work 🙁
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Your code isn’t executing because of jsfiddle. It wrapped your code in an onload handler, thus keeping
doSomethingout of the global namespace. So when setTimeout tried to execute your code, it couldn’t finddoSomething. Change jsfiddle to execute “no wrap”, and all is well: http://jsfiddle.net/gilly3/PRff7/3/If you don’t wrap your call to
doSomethingin a string, it will also work becausesetIntervalgets a direct reference todoSomethingwhich is in the same scope. It doesn’t need a global reference.