Javascript Argument

Javascript Argument

本文关键字:Argument Javascript      更新时间:2023-09-26

我输入滚动(0,10200,10);但当它运行时,它会传递字符串"xxpos"或"yypos",我确实尝试过它,但没有使用同位语,但它就是不起作用。

scroll = function(xpos,ypos,time,rounds){
    var xxpos = xpos*1;
    var yypos = ypos*1;
    var rrounds = rounds*1;
    var ttime = time*1;
    x = 0;
    xyz=window.setInterval("scroller('xxpos','yypos','ttime','rrounds')",ttime);
}
function scroller(xpos,ypos,time,rounds){
    alert(xpos + ypos + time + rounds);
}

不要使用字符串,要使用闭包(匿名函数)。

window.setTimeout(function() {
  scroller(xxpos, yypos, ttime, rrounds);
}, ttime);

应该是这样的:

 xyz=window.setInterval("scroller(" + xxpos + "," + yypos + "...

否则,您只需传递字符串xxpos、yypos等。

您是否知道,在您的代码中,每次调用scroll()都会构建一个计时器?

你的意思是把它当作一个循环吗?然后:

xyz = window.setTimeout(function(){
    scroller(xxpos,yypos,ttime,rrounds)
},ttime);

您应该使用闭包:

...
xyz = window.setInterval(function() { scroller(xxpos,yypos,ttime,rrounds); }, ttime);
...

这是因为字符串不会成为变量。

这将起作用:

window.setInterval("scroller("+ xxpos + "," + yypos + "," + ttime + "," + rrounds + ")",ttime);

或者更好:

window.setInterval(function() { scroller(xxpos, yypos, ttime, rrounds); }, ttime);