Why does setInterval call a function with random arguments?

So, I see a curious problem. If I have a function

// counter wraps around to beginning eventually, omitted for clarity.
var counter; 
cycleCharts(chartId) {
    // chartId should be undefined when called from setInterval
    console.log('chartId: ' + chartId);
    if(typeof chartId == 'undefined' || chartId < 0) {
        next = counter++;
    }
    else {
        next = chartId;
    }
    // ... do stuff to display the next chart
}

This function can be called explicitly by user action, in which case it is chartIdpassed as an argument and the selected diagram is displayed; or it can be in autoplay mode, in which case it calls setInterval, which is initialized as follows:

var cycleId = setInterval(cycleCharts, 10000);

Strange, I actually see that cycleCharts()getting the argument chartId, even if it called out setInterval! setIntervaldoesn't even have parameters to pass the function cycleCharts, so I'm very puzzled by why chartIdnot undefined when cycleChartscalled from setInterval.

+3
source share
3 answers

setInterval - ( , )

 var cycleId = setInterval(function(){ cycleCharts(); }, 10000); 

( , )

+4

, .

+3

var cycleId = setInterval (cycleCharts, 10000, 4242);

- , 4242 chartId. , , ? , , , , / .

+1

Source: https://habr.com/ru/post/1697064/


All Articles