【问题标题】:Fix Firefox throwing error on setInterval. [duplicate]修复 Firefox 在 setInterval 上抛出错误。 [复制]
【发布时间】:2013-03-08 15:32:40
【问题描述】:

为什么setInterval(fetchData(), 1000*60); 在 Chrome 中工作,而在 Firefox 中抛出错误;我想将一个变量传递给 fetchdata。

   function fetchData(var) {
        .....
    };

    fetchData(var); //run once
    setInterval(fetchData(var), 1000*60); //then repeat

堆栈跟踪:

[10:26:49.764] Error: useless setInterval call (missing quotes around argument?)
userController@http://localhost:8000/mainx.js:169
invoke@http://localhost:8000/lib/angular.js:2809
instantiate@http://localhost:8000/lib/angular.js:2819
@http://localhost:8000/lib/angular.js:4639
applyDirectivesToNode/nodeLinkFn/<@http://localhost:8000/lib/angular.js:4218
forEach@http://localhost:8000/lib/angular.js:117
nodeLinkFn@http://localhost:8000/lib/angular.js:4203
compositeLinkFn@http://localhost:8000/lib/angular.js:3851
publicLinkFn@http://localhost:8000/lib/angular.js:3763
bootstrap/</<@http://localhost:8000/lib/angular.js:932
Scope.prototype.$eval@http://localhost:8000/lib/angular.js:7840
Scope.prototype.$apply@http://localhost:8000/lib/angular.js:7920
bootstrap/<@http://localhost:8000/lib/angular.js:930
invoke@http://localhost:8000/lib/angular.js:2802
bootstrap@http://localhost:8000/lib/angular.js:929
angularInit@http://localhost:8000/lib/angular.js:904
@http://localhost:8000/lib/angular.js:14527
p.Callbacks/k@http://localhost:8000/lib/jquery-1.8.2.min.js:2
p.Callbacks/l.fireWith@http://localhost:8000/lib/jquery-1.8.2.min.js:2
.ready@http://localhost:8000/lib/jquery-1.8.2.min.js:2
D@http://localhost:8000/lib/jquery-1.8.2.min.js:2

【问题讨论】:

    标签: javascript google-chrome firefox


    【解决方案1】:

    使用

    setInterval(function() {fetchData(var)}, 1000*60); //then repeat
    

    您所做的实际上根本没有传递您的论点。您需要将其包装在 anonymous function 中。

    【讨论】:

      【解决方案2】:

      此语法不正确。您不能使用 setTimeout 或 setInterval 传递这样的变量。

      setInterval(fetchData(var), 1000*60); //then repeat
      

      请改用以下内容。

      setInterval(function() {
          fetchData(var);
      }, 1000*60);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-13
        • 2021-01-14
        • 1970-01-01
        • 1970-01-01
        • 2018-06-06
        • 2010-11-04
        • 2019-11-13
        相关资源
        最近更新 更多