I type in scroll(0,10,200,10); But when it runs it passes the string "xxpos" or "yypos" and I did try it without the appostraphes, but it just didn't work.
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);
}
Sergio Tulentsev
231k43 gold badges381 silver badges373 bronze badges
5 Answers 5
Don't use strings, use closures (anonymous functions).
window.setTimeout(function() {
scroller(xxpos, yypos, ttime, rrounds);
}, ttime);
answered Mar 26, 2012 at 20:43
Sergio Tulentsev
231k43 gold badges381 silver badges373 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
It should be like this:
xyz=window.setInterval("scroller(" + xxpos + "," + yypos + "...
otherwise you just pass strings xxpos, yypos etc.
answered Mar 26, 2012 at 20:44
Eugene Retunsky
13.2k5 gold badges54 silver badges55 bronze badges
1 Comment
Tanner Ellis
Thanks so much! This worked! I can't believe I didn't think of something so obvious!
do you happen to know that in your code, each call to scroll() builds a timer?
do you mean to do it like it was a loop? then:
xyz = window.setTimeout(function(){
scroller(xxpos,yypos,ttime,rrounds)
},ttime);
answered Mar 26, 2012 at 20:44
Joseph
120k31 gold badges186 silver badges238 bronze badges
Comments
You should use closure:
...
xyz = window.setInterval(function() { scroller(xxpos,yypos,ttime,rrounds); }, ttime);
...
answered Mar 26, 2012 at 20:44
Vadym S. Khondar
1,4482 gold badges12 silver badges19 bronze badges
Comments
That's because the string does not become the variable.
This would work:
window.setInterval("scroller("+ xxpos + "," + yypos + "," + ttime + "," + rrounds + ")",ttime);
Or better:
window.setInterval(function() { scroller(xxpos, yypos, ttime, rrounds); }, ttime);
answered Mar 26, 2012 at 20:47
Ortiga
8,8466 gold badges45 silver badges74 bronze badges
Comments
lang-js
scrollerfunction strings as parameters. Also, afaik, the variables won't be available from the scopesetIntervalwill evaluate the string.x = 0andxyz = ..doing there?xyzis a var from another scope that he need's to stop his interval. But the name is ugly.