So this video is going to be the last one on functions for a little bit.
And we're going to talk about a concept called the higher order functions.
So higher order functions are functions that either take a function as an argument or they return another
function.
So yes this is possible and not only is it possible it's something that we'll be doing a lot especially
later in this course.
So I'm going to show you an initial example something called set interval and what set interval does
is it takes a function and it will call that function at a specific interval that we provide.
So it's a way to have something repeat every second or every half a second.
Right now with a loop we can repeat things but it happens immediately.
So set interval we can time it out.
So let's go ahead and take a look at how set interval works.
And before we actually dive into that we need to define a function that we are going to pass to set
interval.
So we're going to write code to sing Twinkle twinkle little star again.
We're just going to abreviated and we're going to schedule it to run using set interval every second
.
So first we need to write the twinkle twinkle code.
So I'm going to do a function sing and that's just going to come to don't log.
Twinkle twinkle.
And then another line
I wonder and we'll just leave it at that so I can call sing myself.
Just like that.
And that works great.
So next what we want to do is use set interval to make sing or to call sing every 1000 milliseconds
or every second.
So to do that I'm going to define the pattern here so set interval takes two arguments.
It's just a function but it takes another function as its argument.
The first one is just we'll call it another funk.
And the second one is the interval in milliseconds.
So I'm going to fill this out now.
Another phone call the function we want it to call is called Sing and the interval is 1000 milliseconds
.
Or we could do every half a second 500 milliseconds or one millisecond which would be insanely fast
but we're going to do 1000 milliseconds.
So this is going to call whatever's inside of sing these two lines every 1000 milliseconds.
And when I hit Enter you'll see that it starts to happen.
The other thing I just want to call your attention to before it disappears is that when Iran set interval
it returns this number two.
So we'll see what that's used for and just a moment.
So another thing that you might be asking is why do we not need parentheses after seeing.
And the reason for that is that we're not the one calling saying set interval is actually calling saying
every 1000 seconds.
So we don't want to execute it.
We want to let's set interval executed.
So what we do is just pass the value of saying we're just passing the name Singh and the code inside
of it.
But we're not running it.
We leave that to set interval.
So to stop set interval we need to use this number to set interval returns a number and we can use that
number to stop it.
So there's another function clear interval and we just give it that number two or it might be another
number in your case.
So to sum this up set interval is just one example of a higher order function and a higher order function
is one where we can pass another function to it as an argument.
And we could return another function which we haven't seen yet but most often we'll be passing a function
to another function.
So there's one other change we could make which is sometimes we want to run some code every second.
For instance set interval.
But we don't want to define a separate function ahead of time.
So instead of saying here I don't have a function yet and I want to write it right here I could do that
with something called an anonymous function.
And that looks like this.
So I write function but I don't give it a name because I'm never going to use it again.
I'm just giving it to set interval.
So function parentheses and then inside of here I'll write my code and I'm going to hit enter.
So inside of these braces I put my canceled out log.
I am anonymous function and control that log.
This is awesome.
All right.
So then I need to add my interval comma and let's do this every two seconds.
So this is a little bit hectic looking.
Coming from our simple swing set interval what we're doing though is defining the function right here
in line.
So this is not something we can call outside of set interval.
It's purely a way to pass a group of code into set interval.
And then the second argument is the same.
It's just another number.
And if I do this you'll see every two seconds.
It calls this code.
But I have no way to call this function again.
Outside of it.
So we will see anonymous functions quite a bit more later in this class and almost always we use them
when we're passing in a function to another function.
So if this is still a little hazy for you don't worry we're going to spend a lot more time using these
things like set interval.
But also some other functions that we're going to introduce in a few videos from now.