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

时间:2016-03-09 22:39:52

标签: javascript jquery function

我有一个函数,每次都返回不同的值:

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

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

每次调用此函数都会随机改变图像的src。

现在,我有第二个管理多个动画的功能

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

最后一个参数是“func”。如果给出(有时给出,有时不给出),则应使用先前的函数changeImg()随机更改图片。不幸的是,如果我这样称呼它:

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

它会随机拍摄第一个电话,但下一个和所有其他电话仍然相同。如何每次调用changeImg()函数来更改图像?

1 个答案:

答案 0 :(得分:0)

试试这个:

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