vendredi 26 octobre 2018

Problem in my custom setInterval() function in javascript

Here I made a custom setInterval function but it is not working as desired.

function interval(func,ti,f){
if(f==0||f==undefined){
    try{                  //calls the function provided as argument.
        func.call();
    }
    catch(err){                 //if error occurs
        throw err.toString();
        f=1; 
    }
    setTimeout(interval(func,ti,f),ti);
   }
}

the main idea behind this is that the user calls the function somewhat like this:

interval(()=>{
   console.log('hello world');
 },10000);

since I'm not passing the value of f so it is undefined so it satisfies and enters the if statement. now when try{} calls the function, it executes. The problem is that I am calling the SetInterval function by passing arguments as the function interval() and ti which is the time in milliseconds.

So it should call the function interval() after time ti but it is not doing so.

plz, help me out.




Aucun commentaire:

Enregistrer un commentaire