【问题标题】:Understanding bootstrap carousel code了解引导轮播代码
【发布时间】:2015-04-25 00:42:35
【问题描述】:

大家好,我是 JS 和 jQuery 的新手,我刚刚浏览了 carousel.js 的源代码,发现了以下代码行:

this.cycle(true)

现在循环函数看起来像这样:

  Carousel.prototype.cycle = function (e) {

    // console.log('inside cycle');
    e || (this.paused = false)

    this.interval && clearInterval(this.interval)

    this.options.interval
      && !this.paused
      && (this.interval = setInterval($.proxy(this.next, this), this.options.interval))

    return this
  }

如果我console.log(e),我永远不会得到“真实”为什么? 我所指的行可以找到here

有人可以向我解释为什么 this.cycle() 在该行被传递值 true 吗?

谢谢。

【问题讨论】:

  • 这是另一个不以像e || (this.paused = false) 这样糟糕的方式编写代码的原因

标签: javascript jquery twitter-bootstrap carousel


【解决方案1】:

console.log(e) 在循环函数中应该返回 true。但是更容易理解的代码分解是这样的:

Carousel.prototype.cycle = function (e) {

    // console.log('inside cycle');
    //e || (this.paused = false)
    if(!e){
        this.paused=false;
    }

    //this.interval && clearInterval(this.interval)
    if(this.interval){
        clearInterval(this.interval);   
    }

    /*
    this.options.interval
      && !this.paused
      && (this.interval = setInterval($.proxy(this.next, this), this.options.interval))
    */
    if(this.options.interval && !this.paused){
        this.interval = setInterval($.proxy(this.next, this), this.options.interval)
    }
    return this
  }

【讨论】:

    【解决方案2】:

    根据代码,如果您不传递任何内容或传递false,它也会取消暂停轮播。通过true 基本上意味着“不要自动滚动我的轮播”。这段代码很神奇:

    this.paused = false
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-08
      • 2019-05-02
      • 1970-01-01
      • 2018-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      相关资源
      最近更新 更多