循环结束后重新启动jQuery.each() [英] Restart jQuery.each() after loop ends

查看:158
本文介绍了循环结束后重新启动jQuery.each()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个值数组,我想将这些值设置为输入的占位符.

I have array of values and i want to set those values as placeholders to my input.

如何仅使用jQuery.each() 实现此目标,因为我使用

How to achieve this using jQuery.each() only because i solved my issue with this solution and it works perfectly.

我尝试执行此操作以重新启动它,但是它不起作用:

I tried doing this to restart it but it's not working:

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

HTML代码:

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

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

提琴手: http://jsfiddle.net/charaf11/5ZQgX/

推荐答案

尝试以这种方式重新启动.each()循环存在两个问题.首先,这不是.each()真正起作用的方式.在每个元素上调用该函数的过程更少循环,更简捷.如果您给该匿名函数命名(让我们使用setPlaceholder()来命名),则.each()调用实际上就是在这样做:

There are two issues with trying to restart a .each() loop this way. First and foremost, that's not how .each() really works. 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(0, arr[0]);
setPlaceholder(1, arr[1]);
setPlaceholder(2, arr[2]);
setPlaceholder(3, arr[3]);
setPlaceholder(4, arr[4]);
setPlaceholder(5, arr[5]);

它传递给函数的索引值不用于循环目的,因此尝试将其设置为0不会对.each()调用产生任何影响.

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.

第二个问题是您的if条件.它永远不会触发,因为.each()调用的最后迭代"将以arr.length - 1作为其值,而不是arr.length.

The second issue is your if condition. It'll never actually fire, since the final "iteration" of the .each() call will have arr.length - 1 as its value, not 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

应该做的是在上次占位符更新发生1秒钟后再次调用计划eachChange().您可以添加其他一些检查来限制其重复发生的次数,但是如果您希望它无限期地发生,那应该可以解决问题.

What that should do is schedule eachChange() to be called again 1 second after the last placeholder update takes place. 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.

这是演示它的更新小提琴.

这篇关于循环结束后重新启动jQuery.each()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆