简体   繁体   English

循环结束后重新启动jQuery.each()

[英]Restart jQuery.each() after loop ends

I have array of values and i want to set those values as placeholders to my input. 我有值数组,我想将这些值设置为我的输入的占位符。

How to achieve this using jQuery.each() only because i solved my issue with this solution and it works perfectly. 如何使用jQuery.each()实现此目的是因为我使用此解决方案解决了我的问题,并且效果很好。

I tried doing this to restart it but it's not working: 我尝试这样做以重新启动它,但是它不起作用:

if(index==arr.length) index=0;

HTML code: HTML代码:

 Values : <input name='input' id='input' />

JS/jQuery code: JS / jQuery代码:

var arr = new Array();
for (var i = 0; i <= 5; i++) {
    arr.push('Value ' + i);//fill array with values
}

function eachChange(){
   var x=0;
   $.each(arr, function (index, value) {
       x++;
   setTimeout(function(){ 
       $('input').attr('placeholder', value);
      }, x * 1000); 
       if(index==arr.length) index=0;

 });   

}
eachChange();//call function

Fiddle: http://jsfiddle.net/charaf11/5ZQgX/ 小提琴: http : //jsfiddle.net/charaf11/5ZQgX/

you can compare the index with the arr.length like this 您可以像这样将索引与arr.length进行比较

var arr = new Array();
for (var i = 0; i <= 5; i++) {
    arr.push('Value ' + i);//fill array with values
}

function eachChange(){
$.each(arr, function (index, value) {
setTimeout(function(){ 
   $('input').attr('placeholder', value);
   //if it is the last element in arr setTimeout and call eachChange() 
   if(index>=arr.length-1){
        setTimeout(function(){  
           eachChange();     
        },1000);
   }

  }, index * 1000); 
});   

}
eachChange();     

http://jsfiddle.net/R274P/1/ http://jsfiddle.net/R274P/1/

There are two issues with trying to restart a .each() loop this way. 尝试以这种方式重新启动.each()循环存在两个问题。 First and foremost, that's not how .each() really works. 首先,这不是.each()真正起作用的方式。 It's less a loop and more shorthand for calling the function on every element. 在每个元素上调用该函数的过程更少循环,更简捷。 If you gave that anonymous function a name (let's go with setPlaceholder() ), the .each() call is essentially doing this: 如果您给该匿名函数命名(让我们使用setPlaceholder() ),那么.each()调用实际上就是在这样做:

setPlaceholder(0, arr[0]);
setPlaceholder(1, arr[1]);
setPlaceholder(2, arr[2]);
setPlaceholder(3, arr[3]);
setPlaceholder(4, arr[4]);
setPlaceholder(5, arr[5]);

The index value it passes to the function isn't used for looping purposes, so trying to set it to 0 won't have any impact on the .each() call. 它传递给函数的索引值不用于循环目的,因此尝试将其设置为0不会对.each()调用产生任何影响。

The second issue is your if condition. 第二个问题是您的if条件。 It'll never actually fire, since the final "iteration" of the .each() call will have arr.length - 1 as its value, not arr.length . 它实际上不会触发,因为.each()调用的最终“迭代”将以arr.length - 1作为其值,而不是arr.length

I'm not sure why you want to have it keep looping, but if that's your goal, you could accomplish it like this: 我不确定为什么要让它保持循环,但是如果这是您的目标,则可以这样实现:

function eachChange(){
    $.each(arr, function (index, value) {
        setTimeout(function() {
            $('input').attr('placeholder', value);
        }, index * 1000);
        if (index == arr.length - 1) {
            setTimeout(eachChange, (index + 1) * 1000);
        }
    });   

}
eachChange();//call function

What that should do is schedule eachChange() to be called again 1 second after the last placeholder update takes place. 要做的是安排在最后一次占位符更新发生后1秒钟再次调用eachChange() You can add in some other checks to limit the number of times it recurses, but if you want it to happen indefinitely that should do the trick. 您可以添加一些其他检查来限制其重复发生的次数,但是如果您希望它无限期地发生,那应该可以解决问题。

Here's an updated fiddle demonstrating it. 这是一个演示它的更新小提琴。

I dont think you can reset the counter of the $.each function, as it seems to encapsulate a simple for loop. 我不认为您可以重置$ .each函数的计数器,因为它似乎封装了一个简单的for循环。 You dont have access to the counter from the outside. 您没有从外部访问柜台的权限。

However, try: 但是,请尝试:

function eachChange(initX){
   var x = initX || 0;
   $.each(arr, function (index, value) {
       x++;
       setTimeout(function(){ 
          $('input').attr('placeholder', value);
       }, x * 1000); 

       // Call yourself but pass current x
       if(index==arr.length) eachChange(x); 

   });   
}

Remove the initX part if you want x to reset, but i assume you want it to keep counting from where the previous loop finished. 如果您想重置x,请删除initX部分,但我想您希望它继续从上一个循环的结束位置开始计数。

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

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