简体   繁体   English

Javascript setInterval没有使其起作用

[英]Javascript setInterval not making it into function

Ok Im trying to do a setInterval into a sub function and its not making it in there...my alert is not firing off because of this: 好的,我试图将setInterval放入子函数中并且没有使其进入那里...因此我的警报没有触发:

var doneVar= 0;
var groupsVar= 4;
var interval = setInterval(process_chunk, 1000);
var $myTree= $("#myTree");
var chunkLength = myArray.length / groupsVar;


process_chunk = function() {
    alert("we are after chunk");
    var arrayChunk = myArray.slice(doneVar*chunkLength, (doneVar + 1)*chunkLength); 
    //alert("we are in function!!");
    $.each(arrayChunk,  function(key, item){
        $myTree.jstree("uncheck_node", "#"+item);
    });

    doneVar += 1;

    if (doneVar === groupsVar) {
        interval.clearInterval();
    }
}

process_chunk has not been assigned a value yet, when you pass it into setInterval. 当您将其传递给setInterval时,尚未为process_chunk分配值。 Move the line: 移动线:

var interval = setInterval(process_chunk, 1000);

To right before (and right after the anonymous function is assigned to process_chunk ): 在将匿名函数分配给process_chunk之前(之后):

doneVar += 1;

Or if you are looking for hoisting the function then use a function declaration rather than an expression: 或者,如果您要提升函数,请使用函数声明而不是表达式:

function process_chunk() {

Both versions will solve your problem. 两种版本都可以解决您的问题。

You need to declare the function before using it. 您需要在使用函数之前声明该函数。

Put process_chunk = function() { ... }); process_chunk = function() { ... });

Before var interval = setInterval(process_chunk, 1000); 之前var interval = setInterval(process_chunk, 1000);

If you are only using the function from the interval use this (my preferred method, your way is not wrong) 如果您仅在间隔内使用函数,请使用此方法(我的首选方法,您的方法没有错)

var doneVar= 0;
var groupsVar= 4;
var $myTree= $("#myTree");
var chunkLength = myArray.length / groupsVar;

var interval = setInterval(function() {
    alert("we are after chunk");
    var arrayChunk = myArray.slice(doneVar*chunkLength, (doneVar + 1)*chunkLength); 
    //alert("we are in function!!");
    $.each(arrayChunk,  function(key, item){
         $myTree.jstree("uncheck_node", "#"+item);
    });
    doneVar += 1;

    if (doneVar === groupsVar) {
         interval.clearInterval();
    }
},1000);

that should do it, doing it this way and defining the function within the interval prevents many problems, like in this case you need to defined the function before you set it in an interval. 应该这样做,以这种方式执行并在间隔内定义函数可以避免很多问题,例如在这种情况下,您需要先定义函数,然后再将其设置为间隔。 Here is another version keeping your style. 这是另一个保留您风格的版本。

var doneVar= 0;
var groupsVar= 4;
var $myTree= $("#myTree");
var chunkLength = myArray.length / groupsVar;

var process_chunk = function() {
     alert("we are after chunk");
     var arrayChunk = myArray.slice(doneVar*chunkLength, (doneVar + 1)*chunkLength); 
     //alert("we are in function!!");
     $.each(arrayChunk,  function(key, item){
          $myTree.jstree("uncheck_node", "#"+item);
     });
     doneVar += 1;

     if (doneVar === groupsVar) {
          interval.clearInterval();
     }
}
var interval = setInterval(process_chunk, 1000);

I guess one other thing I noticed just now, you use process_chuck and not var process_chuck witch can cause problems too. 我想我刚才注意到的另一件事是,您使用process_chuck而不是var process_chuck witch也可能导致问题。 Fixed in second answer, not applicable in first. 已在第二个答案中修复,不适用于第一个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM