Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript - how to restart a function from inside it?

How do I restart a function calling it from inside the same function?

like image 927
FernandoSBS Avatar asked Dec 08 '22 03:12

FernandoSBS


2 Answers

Just call the function again, then return, like this:

function myFunction() {
  //stuff...
  if(condition) {
    myFunction();
    return;
  }
}

The if part is optional of course, I'm not certain of your exact application here. If you need the return value, it's one line, like this: return myFunction();

like image 139
Nick Craver Avatar answered Dec 14 '22 22:12

Nick Craver


Well its recommended to use a named function now instead of arguments.callee which is still valid, but seemingly deprecated in the future.

// Named function approach
function myFunction() {
    // Call again
    myFunction();
}

// Anonymous function using the future deprecated arguments.callee
(function() {
    // Call again
    arguments.callee();
)();
like image 23
tbranyen Avatar answered Dec 14 '22 20:12

tbranyen