1

I want to make a function being called in a certain interval of time inside the constructor. But I don't want to write the function inside the $interval statement because it's a little big. So I wanted to make something like this:

this.$interval(this.getSomething(), 1000);

And the initialize it outside of the controller's constructor:

getSomething = function getSomething(){
                 ...
};

But it gives me an error of TypeError: fn is not a function When it seems to me as a function... Any suggestions?

7
  • 1
    According to AngularJS $interval documentation it expects a callback. Changing your interval part to: this.$interval(this.getSomething, 1000); should work. (()removed) Commented Dec 18, 2018 at 10:19
  • @dquijada — Depends on if getSomething makes use of this internally or not. Commented Dec 18, 2018 at 10:20
  • Well, yes, @Quentin; but that's a more specific case. The only answer below can be checked for that case Commented Dec 18, 2018 at 10:23
  • 1
    @DavidCoelho — See this and this Commented Dec 18, 2018 at 10:31
  • 1
    And btw, @dquijada solution worked just fine. Thanks for the help :D Commented Dec 18, 2018 at 10:37

1 Answer 1

-1

Try changing your function declaration to this.

getSomething = function(){
                 ...
};

and call your interval part like this.

this.$interval(function(){ this.getSomething() }, 1000);
Sign up to request clarification or add additional context in comments.

2 Comments

Using an anonymous function expression instead of a named function expression will not make any difference (unless the function tries to call itself recursively, in which case it will break it).
function(){ this.getSomething() } won't work because the this context will have changed between functions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.