Tanner Ellis
Tanner Ellis

Reputation: 39

Javascript Argument

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);
}

Upvotes: 1

Views: 99

Answers (5)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230306

Don't use strings, use closures (anonymous functions).

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

Upvotes: 6

Ortiga
Ortiga

Reputation: 8814

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);

Upvotes: 1

Vadym S. Khondar
Vadym S. Khondar

Reputation: 1438

You should use closure:

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

Upvotes: 1

Joseph
Joseph

Reputation: 119837

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);

Upvotes: 1

Eugene Retunsky
Eugene Retunsky

Reputation: 13139

It should be like this:

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

otherwise you just pass strings xxpos, yypos etc.

Upvotes: 2

Related Questions