search

Home  >  Q&A  >  body text

javascript - Native JS and jQuety about setting image carousel timer

Regarding the image carousel, setting up automatic playback issues:
If you use native JS code, I currently know two methods, the first one:

function next(){
    这里是代码,比如index++;
}
var timer=setInterval(next, 2000);

The second type (where oBtnNext refers to a button obtained):

oBtnNext.onclick=function(){
    这里是代码,比如index++;
}   
var timer=setInterval(oBtnNext.onclick,2000);

Here comes the point. The question is, if jQuery($("#next") is equivalent to oBtnNext above)

$("#next").click(function(){
    这里是代码,比如index++;
}

Why write

var timer=setInterval($("#next").click,2000)

or

var timer=setInterval($("#next").click(),2000)  

is not allowed, but

var timer=setInterval(function () {
    $("#next").click()
},2000); 

But it works.

大家讲道理大家讲道理2707 days ago1042

reply all(1)I'll reply

  • 漂亮男人

    漂亮男人2017-07-05 11:00:00

    You still don’t understand the usage of setInterval
    setInterval(code, millisec[,"lang"])
    The code is the function to be called or the code string to be executed.
    When using code strings, add "" to turn the method into a string..
    (See w3school for details)

    oBtnNext.onclick=function(){}
    var timer=setInterval(oBtnNext.onclick,2000);

    This is easy to use because oBtnNext.onclick is a function

    var timer=setInterval($("#next").click,2000)

    or

    var timer=setInterval($("#next").click(),2000)

    It doesn’t work, because these two are neither functions nor code strings. You can try writing

    var timer=setInterval('$("#next").click()',2000)

    reply
    0
  • Cancelreply