1

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
asked Mar 26, 2012 at 20:41
4
  • You give the scroller function strings as parameters. Also, afaik, the variables won't be available from the scope setInterval will evaluate the string. Commented Mar 26, 2012 at 20:44
  • What are the undeclared x = 0 and xyz = .. doing there? Commented Mar 26, 2012 at 20:44
  • Maybe the xyz is a var from another scope that he need's to stop his interval. But the name is ugly. Commented Mar 26, 2012 at 20:47
  • The x was in there because I'm using it to see how many times the function has been ran, you just can't see that part and xyz is used to stop it. I'm just learning all of this, so sorry if it looks so horrible. I'm just practicing making a javascript framework in my free time after school. Commented Mar 26, 2012 at 21:09

5 Answers 5

6

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

window.setTimeout(function() {
 scroller(xxpos, yypos, ttime, rrounds);
}, ttime);
answered Mar 26, 2012 at 20:43
Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

Thanks so much! This worked! I can't believe I didn't think of something so obvious!
1

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

Comments

1

You should use closure:

...
xyz = window.setInterval(function() { scroller(xxpos,yypos,ttime,rrounds); }, ttime);
...
answered Mar 26, 2012 at 20:44

Comments

1

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.