2

So I have a timer that looks like this

my_timer = setInterval(function(){
    do_something_amazing();
    do_more.stuff_here();
    var etc_etc = "foo" + bar;
}, 1000);

And I want it to run immediately, and every second after that. I tried appending () to the end of the function, but that caused it to run only once (because it is not returning the function itself to the setInterval?). I then tried to return this; so that it would possibly return the function itself, but that did no good either. That was a random guess.

Is there any way I can get this to work without creating a named function? Thanks!

Shane Reustle
  • 8,633
  • 8
  • 40
  • 51
  • About appending `()`: indeed, you're setting the returned value as a parameter, not a reference to the function. BTW, why don't you want to create a named function? – Marcel Korpel Dec 20 '10 at 19:19
  • No reason really, I was just hoping to learn some fancy new trick to call an anonymous function immediately, while having it return itself. – Shane Reustle Dec 20 '10 at 19:26

3 Answers3

5

Use arguments.callee:

my_timer = setInterval((function(){     
    do_something_amazing();     
    do_more.stuff_here();     
    var etc_etc = "foo" + bar;
    return arguments.callee; 
})(), 1000); 
Shurdoof
  • 1,719
  • 13
  • 16
  • Isn't callee deprecated? https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/arguments/callee – Shane Reustle Dec 20 '10 at 19:30
  • If i understand correctly, only when used like " functionName.arguments.callee". – Shurdoof Dec 20 '10 at 19:34
  • From MDC: "callee as a property of Function.arguments is no longer used.", says nothing about it being deprecated when used in a function. – Shurdoof Dec 20 '10 at 19:51
  • @Shane: Shurdoof is right; `arguments.callee` is only explicitly forbidden when in ES262-5 [strict mode](http://ecma262-5.com/ELS5_Annex_C.htm). – Marcel Korpel Dec 20 '10 at 19:57
  • +1 cleaner and cleverer than my answer.. i didn't know about `callee` – Emmett Dec 20 '10 at 20:11
3

This doesn't technically create any named functions, but I'm not sure why you'd want to do it:

(function(func) {
    func();
    setInterval(func, 1000);
})(function() {
    do_something_amazing();
    do_more.stuff_here();
    var etc_etc = "foo" + bar;
});
Emmett
  • 14,035
  • 12
  • 56
  • 81
2

Would this work for you?

function startItUp()
{
   bla();
   setInterval( bla, 1000);
}

function bla()
{
    do_something_amazing();
    do_more.stuff_here();
    var etc_etc = "foo" + bar;
}

Hope this helps,

Marvin Smit
  • 4,088
  • 1
  • 22
  • 21
  • It works, but the OP asked: “Is there any way I can get this to work **without creating a named function**?” Moreover, I'd change the order of `bla()` and `setInterval()`: if `bla` takes some time, there will be a difference in timing between the first two invocations and the next ones. See [this nice answer](http://stackoverflow.com/questions/729921/settimeout-or-setinterval/731625#731625) to read how `setInterval` works. – Marcel Korpel Dec 20 '10 at 19:23