繁体   English   中英

Javascript函数在控制台中显示“未定义”

[英]Javascript function shows “undefined” in console

我有一排图像,我想一次闪烁一次。 几年前,我在一个旧站点上使用了下面的代码,并且想重用这些代码,但是我无法在新站点上使用它。

$('document').ready(function() {

    var myArray = ["img.square1","img.square2","img.square3","img.square4","img.square5","img.square6","img.square7","img.square8","img.square9","img.square10","img.square11","img.square12","img.square13","img.square14","img.square15"];

    console.log(myArray)
    var count = 15;
    function counter() {
        if(count < 16 && count > -1){
            $(myArray[count]).fadeTo(100, 0.1, function(){
                $(myArray[count]).fadeTo(100, 0.7, function(){
                    $(myArray[count]).fadeTo(200, 0.5, function(){
                        $(myArray[count]).fadeTo(500, 1);
                        count--
                console.log(count);
                    });                                      
                });

            });

        }else{
            clearInterval(myInterval);  
            //console.log("interval cleared");
        }
    }
    //counter();
    try{
        counter();
        alert('I guess you do exist');
    }
    catch(e){
            alert('An error has occurred: '+e.message);
    }

    console.log(counter());
    var myInterval = setInterval(counter(), 1100);
});

您可以使用.queue().fadeTo()回调代替setInterval()

function counter(selector) {
  return $(selector).queue("images", $.map($(selector), function(image) {
    // `next` is a reference to the next function in `queueName`: `"images"`
    return function(next) { 
      return $(image).fadeTo(100, 0.1, function() {
        $(this).fadeTo(100, 0.7, function() {
          $(this).fadeTo(200, 0.5, function() {
            $(this).fadeTo(500, 1, next);
          });                                      
        });
      });
    }
  })).dequeue("images").promise("images")
}

counter("img[class^=square]")
.then(function() {
  console.log("done", this)
})

暂无
暂无

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

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