javascript - Native JS and jQuety about setting image carousel timer
大家讲道理
大家讲道理 2017-07-05 10:58:00
0
1
1053

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.

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(1)
漂亮男人

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)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template