简体   繁体   English

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

[英]Javascript function shows “undefined” in console

I have a grid of images that I want to blink on and off one at a time once through. 我有一排图像,我想一次闪烁一次。 I had the below code working a few years ago on an old site and wanted to reuse the code but I cannot get it working on the new site. 几年前,我在一个旧站点上使用了下面的代码,并且想重用这些代码,但是我无法在新站点上使用它。

$('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);
});

You can substitute using .queue() and .fadeTo() callback for setInterval() 您可以使用.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.

相关问题 JavaScript pythagoras函数显示为未定义 - Javascript pythagoras function shows as undefined JavaScript函数在控制台中返回未定义 - JavaScript function returning undefined in console Javascript返回html标记中未定义的内容,但在开发人员控制台中显示结果 - Javascript returns undefined in html tags but shows result in developer console JavaScript-Console.log返回一些内容,但返回时显示未定义 - JavaScript - Console.log return something but with return it shows undefined OOO javascript:object 自参考返回未定义但控制台显示它存在 - OOO javascript: object self reference returns undefined but console shows it exists Javascript未定义错误,但控制台日志显示了这些值 - Javascript undefined error yet console log shows the values 异步函数返回值,显示在控制台上,但在其他地方未定义 - Async function returns value, shows up on console, but undefined elsewhere JavaScript 函数返回未定义 + 控制台日志 - JavaScript function returning undefined + console log 从控制台未定义JavaScript函数,无可见响应 - JavaScript function undefined from console no visible response 调用javascript函数,值在控制台中返回为undefined - Calling a javascript function, value returns as undefined in the console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM