Why does jsHint say "setInterval" is not defined "

When checking my * .js with jshint, an error is displayed in it:

function updateStatistic(interval) { return setInterval(function () { exports.getStatistics(); }, interval); } 

Message: 'setInterval' is undefined. But why?

+6
source share
3 answers

Alternatively, you can simply use JSHint for the browser:

 /*jshint browser: true */ 

( Link )

+10
source

You must tell jshint which object and / or functions are considered global in the jshint tag comment at the top of the code. jsHint does not imply any changes, as they depend on the particular environment in which the code is executed.

The approach I'm using is to tell jshint that the window object itself is global (and several others) with the following, as well as the ES5 strict mode directive:

 /*global $ document window localStorage */ "use strict"; 

Having blocked the warning about setInterval , then the prefix of calling this function with the window. is required window. - I like to explicitly specify the global functions that I use, so I do not consider this a drawback.

+6
source

if you do not want to configure your settings in jshint, there is nothing wrong with simply referencing it through a window object, for example:

 function updateStatistic(interval) { return window.setInterval(function () { exports.getStatistics(); }, interval); } 

This will also remove the jshint warning as it is defined in this context.

+1
source

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


All Articles