The second argument to the setInterval timer function is a variable

When the second parameter was variable, the variable changed, but I found that the wait time for execution did not change at all. It was the same as the first time, so I wrote a few lines of code to test it. SetInterval () is set to a fixed amount of time once it’s started. It doesn’t change.

// Initial wait time
let waitDate = 1000;

// Growing up Xiao Ming
const GrowingPeople = {name: 'Ming'.age: 14.sex: 'male'};

// The aging function
function ageIncrease(people) {
    const { age } = people;
    if(age && age >= 18) {return true;
    } else {
        people.age = people.age + 1;
        return false;
    };
}

// Prompts the adult function
function promptAdulthood(people) {
    const { name='xiao gang', age=15, sex='woman' } = people;
    const information = I call `${name}That is a${sex}This year,${age}Age, adult, can go to the Internet;
    console.log(information);
}

// in version 1, it was found that the set time did not take effect
 function simplePoller(queryFn, callback) {
     const stop = setInterval(() = > {
     const res = queryFn(GrowingPeople);
         if(res){
             callback(GrowingPeople);
             clearInterval(stop);
         } else {
             console.log(I didn't `${GrowingPeople.age}Age, not yet adult ');
         }
     }, waitDate * 1.5);
 }
 simplePoller(ageIncrease, promptAdulthood);
Copy the code

Had meant to be each perform a, next time the execution time is 1.5 times the execution time of the last time, but start after every execution time is 1 second, I think should be the setInterval () at the time of initialization is fixed on the execution time, no matter how you after the variable changes, it is according to the initialization time to give value to perform regularly, So if you want to change the timing, this is not a good way to do it, so let’s do a recursive call instead.

// Initial wait time
let waitDate = 1000;

// Growing up Xiao Ming
const GrowingPeople = {name: 'Ming'.age: 14.sex: 'male'};

// The aging function
function ageIncrease(people) {
    const { age } = people;
    if(age && age >= 18) {return true;
    } else {
        people.age = people.age + 1;
        return false;
    };
}

// Prompts the adult function
function promptAdulthood(people) {
    const { name='xiao gang', age=15, sex='woman' } = people;
    const information = I call `${name}That is a${sex}This year,${age}Age, adult, can go to the Internet;
    console.log(information);
}
function simplePoller(queryFn, callback, waitDate{
    const stop = setInterval(() = > {
        const res = queryFn(GrowingPeople);
        if(res){
            clearInterval(stop);
            callback(GrowingPeople);
        } else {
            clearInterval(stop);
            console.log('Next time you ask about the waiting time,${waitDate}`);
            simplePoller(queryFn, callback, waitDate*1.5)
        }
    }, waitDate);
}

simplePoller(ageIncrease, promptAdulthood, waitDate);
Copy the code

This does increase the wait time by 1.5 times, but it feels like a waste of performance to create a new timer every time the function is executed and then destroy it. Since setInterval() cannot use a changed value for its second argument, it doesn’t make much sense to use it. Instead, use recursion and setTimeout(). You don’t have to clear the timer every time

function simplePoller(queryFn, callback, waitDate{
    setTimeout(() = > {
        const res = queryFn(GrowingPeople);
        if(res){
            callback(GrowingPeople);
        } else {
            console.log('Next time you ask about the waiting time,${waitDate}`);
            simplePoller(queryFn, callback, waitDate*1.5)
        }
    }, waitDate);
}

simplePoller(ageIncrease, promptAdulthood, waitDate);
Copy the code

Attached is the printed record of the last execution.

This looks a lot cleaner, and although it doesn’t solve the problem of setting the parameters of the setInterval function, it can be done in a different way.

At present, it is still a front-end side menu, I did not find the source of setInterval function, I hope there are big guys can help to see if there is a problem with my writing method, there is any better way, you can also leave a message, exchange it.