将函数作为参数传递给递归 setTimeout

Pass function as parameter to recursive setTimeout

此代码无效:

  var counter = 0;

  kick(print);

  function kick(f) {
    setTimeout(function(){
      f();  // problem here;
      kick();
    }, 500);
  }

  function print(){
    console.log(counter++);
  }      

浏览器控制台出现以下错误:

Uncaught TypeError: f is not a function

如何在需要的地方正确调用 f()

再过 f 轮:

function kick(f) {
   setTimeout(function(){
      f();  // problem here;
      kick(f);
    }, 500);
 }