pooyan pooyan - 13 days ago 6
Javascript Question

Send interval id as an argument to executing function

Is it possible to send interval id to executing function as an argument ? For example some thing like this:

var id= setInterval(myFunc.bind(null,id),1000);


What I am going to do is that in
myFunc
I want to do some processing and clear interval if I need. I can't use global variable because it will be happened multiple time. And I know that I can use global array but it would a little a bit of time consuming because of my function logic. So I want to know if I can pass interval id as an argument to
myFunc
.

EDIT:
this stackOverfllow link dosen't help me because there were no helpful answer.

Answer

I use this pattern:

function myFunc(host){
   console.log(host.id);
}

var host = {};
host.id = setInterval(myFunc.bind(null,host),1000);