简体   繁体   English

jQuery-将一个函数作为参数传递给另一个函数,每次调用一个函数时检查返回值

[英]jquery - pass a function as argument to another function, check for return value each time calling a function

I have a function that each time returns different value: 我有一个函数,每次返回不同的值:

this.imageArr = [ "1.png", "2.png" ]

this.changeImg = function () {
    return $( "#Img" ).attr("src", "images/" + this.imageArr[Math.floor(Math.random()*this.imageArr.length)]);
}

This function each time it is called changes randomly src of image. 每次调用此函数都会随机更改图像的src。

now, i have second function that manages multiple animation 现在,我有第二个功能可以管理多个动画

$.fn.animationNSteps = function ( className, delay, steps, infinite, visibility, func ) {
      [...]
      $(this).animationNSteps( className, delay, steps, infinite, visibility, func )
}

Last parameter is "func". 最后一个参数是“ func”。 If it is given (it sometimes is given, sometimes is not) it should randomly change picture using previous function changeImg() . 如果给出(有时给出,有时不给出),则应使用先前的函数changeImg()随机更改图片。 Unfortunately if I call it like this: 不幸的是,如果我这样称呼它:

$("#title").animationNSteps ( class, 3000, 3, true, true, obj.changeImg )

It randomly takes picture for the first call, but next one and all the others are still the same. 它为第一个呼叫随机拍照,但下一个和所有其他呼叫仍然相同。 How to call changeImg() function each time to change the image? 每次如何调用changeImg()函数来更改图像?

Try this: 尝试这个:

$("#title").animationNSteps ( 
    class, 3000, 3, true, true, 
    function(){
        return obj.changeImg();
    } )

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

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