Hello.

Trying to loop the javascript execution. But I get the error -

Uncaught ReferenceError: test is not defined at: 1: 1.

What did you do wrong?

function test() { console.log('test_'); setTimeout('test()', 1000); } test(); 

    2 answers 2

    setTimeout takes a function and a time as arguments. You passed a string. It should be like this

     function test() { console.log('test_'); setTimeout(test, 1000); } test(); 

    • 2
      The string can also be passed. But it runs in the global scope. - Igor

    Your code is inside another function. Because of this, the test function is not visible in the global scope.

     function starter() { function test() { console.log('test_'); setTimeout('test()', 1000); } test(); } starter(); 

    Without a wrapper, it works:

     function test() { console.log('test_'); setTimeout('test()', 1000); } test(); 

    Therefore, it is better to submit to setTimeout not a line of code, but a link to a function. This works regardless of the presence / absence of a wrapper:

     function starter() { function test() { console.log('test_'); setTimeout(test, 1000); } test(); } starter(); 

    • I understand now the error. Thank you)) - LexXy