Why jsHint says "'setInterval' is not defined"
Asked Answered
I

3

10

When I check my *.js with jshint, it's show an error on this part:

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

The message is: 'setInterval' is not defined. But why?

Isom answered 7/8, 2014 at 7:17 Comment(0)
K
15

Alternatively, you could just have JSHint assume a browser:

/*jshint browser: true */

(Reference)

Katar answered 7/8, 2014 at 7:32 Comment(0)
W
5

You have to tell jshint which object and/or functions are considered global in the jshint tag comment at the top of your code. jsHint doesn't assume any, since they vary depending on the precise environment in which the code is run.

The approach I use is to tell jshint that the window object itself is global (and a few others) with the following, and an ES5 strict-mode directive thrown in too:

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

Silencing the warning about setInterval then requires prefixing that function call with window. - I like having to explicitly tag the global functions I'm using so I don't consider this a disadvantage.

Wu answered 7/8, 2014 at 7:21 Comment(0)
A
1

if you don't want to adjust your settings in jshint, there is nothing wrong with simply referencing it through the window object, like so:

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

That will also remove the jshint warning, as it is defined within that context.

Autoharp answered 18/12, 2017 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.