Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript setTimeout在$内。每个韩元';我不尊重时机_Javascript_Jquery_Settimeout_Each_Timing - Fatal编程技术网

Javascript setTimeout在$内。每个韩元';我不尊重时机

Javascript setTimeout在$内。每个韩元';我不尊重时机,javascript,jquery,settimeout,each,timing,Javascript,Jquery,Settimeout,Each,Timing,我想备份、删除列表中的每个项目,并在1秒后追加每个项目 我试着这样做: var backup = $('#rGallery').html(); $('#rGallery li').remove(); console.log($(backup).filter('li').length); /* it logs like 135 */ $(backup).filter('li').each(function(){ var the = $(this); var timeout = se

我想备份、删除列表中的每个项目,并在1秒后追加每个项目

我试着这样做:

var backup = $('#rGallery').html();
$('#rGallery li').remove();
console.log($(backup).filter('li').length); /* it logs like 135 */
$(backup).filter('li').each(function(){
    var the = $(this);
    var timeout = setTimeout(function(){
        console.log(the.html()); /* it logs the html, but all at the same time*/
        $('#rGallery').append('<li>'+the.html()+'</li>');

        /*return*/
    },1000);
});
var backup=$('#rGallery').html();
$('rGallery li')。删除();
console.log($(backup.filter('li').length);/*它的记录像135*/
$(备份).filter('li')。每个(函数(){
var=美元(此);
var timeout=setTimeout(函数(){
console.log(the.html());/*它记录html,但同时记录所有内容*/
$(“#rGallery”).append(“
  • ”+the.html()+“
  • ”); /*返回*/ },1000); });
    它的工作,项目被删除,然后再次追加,问题是,他们都被追加后,1秒。而不是每一个等待1秒从上一个


    我在这里遗漏了什么?

    setTimeout
    没有阻塞;它安排超时,然后继续。因此,
    each()
    的下一次迭代将立即进行,这将为完全相同的时间安排下一个元素(etc)

    因此,您应该像这样更改代码(一种方法),每1000毫秒运行一次函数,并将下一个元素添加到
    备份中

    var backup = $('#rGallery li');
    var i = 0;
    $('#rGallery li').remove();
    
    var interval = setInterval(function () {
        if (i === backup.length) {
            clearInterval(interval);
        } else {
            var the = $(backup).eq(i);
    
            $('#rGallery').append('<li>'+the.html()+'</li>');
        }
    
        i++;
    }, 1000);
    
    var backup=$('rGallery li');
    var i=0;
    $('rGallery li')。删除();
    var interval=setInterval(函数(){
    if(i==backup.length){
    间隔时间;
    }否则{
    var=$(备份).eq(i);
    $(“#rGallery”).append(“
  • ”+the.html()+“
  • ”); } i++; }, 1000);
    因为您告诉他们所有人在一秒钟内运行,所以他们不会被添加到某个神奇的队列中,并排队等待开始计数

    $(backup).filter('li').each(function(index){  //added index here
        var the = $(this);
        var timeout = setTimeout(function(){
            console.log(the.html()); /* it logs the html, but all at the same time*/
            $('#rGallery').append('<li>'+the.html()+'</li>');
    
            /*return*/
        },1000*(index+1)); //multiplied it here
    });
    
    $(备份).filter('li')。每个(函数(索引){//在此处添加了索引
    var=美元(此);
    var timeout=setTimeout(函数(){
    console.log(the.html());/*它记录html,但同时记录所有内容*/
    $(“#rGallery”).append(“
  • ”+the.html()+“
  • ”); /*返回*/ },1000*(索引+1));//在这里乘以它 });
    return呢..我不知道还有什么可以尝试。。但是没有它的结果是一样的。每个循环将在几毫秒内设置所有超时,一秒钟后所有超时都将执行。需要rivese代码来调用一次传递一个元素的函数。请尝试jquery detach();而不是制作一些html副本