...

View Full Version : Arguments in setInterval func



Bobo
07-12-2004, 05:04 PM
I have a script with a function that triggers a 'setInterval' but the function the interval triggers has an arguement. How can I do this?
Note:the function is also triggered before the interval starts so that there is no delay.
This is the part of the code I'm talking about.
(turnI is the function to be triggered.)


.....
function turny(ITaa) {
var Gunck=turnI(ITaa);
Timer=setInterval(Gunck,503)
}
.....
:confused:

Basscyst
07-12-2004, 06:26 PM
I can't be sure because you are only posting part of the code but from what you are showing this may answer your question.



<html>
<head>
<script type="text/javascript">
function turnI(ITaa)
{
alert();
x=ITaa+1;
alert(x);
}

function turny(ITaa) {
Timer=setInterval("turnI("+ITaa+")",503)
}

</script>
</head>
<body onload="turny(2)">
</body>
</html>


Basscyst

glenngv
07-13-2004, 10:36 AM
I have a script with a function that triggers a 'setInterval' but the function the interval triggers has an arguement. How can I do this?
Note:the function is also triggered before the interval starts so that there is no delay.
This is the part of the code I'm talking about.
(turnI is the function to be triggered.)


.....
function turny(ITaa) {
var Gunck=turnI(ITaa);
Timer=setInterval(Gunck,503)
}
.....
:confused:


function turny(ITaa) {
var Gunck=turnI(ITaa);
Timer=setInterval(function(){turnI(ITaa)},503);
}

But if you want to support IE5, do what Basscyst suggested. But if iTaa is a string and not a number, you have to surround it with quotes.


function turny(ITaa) {
Timer=setInterval("turnI('"+ITaa+"')",503)
}

But the difficult part of that is if the argument has single quotes in it, you have to replace ' with \'. The same is true if you use double quotes as string delimiter and the argument has double quotes in it.

Bobo
07-13-2004, 05:15 PM
I want IE5 supported so I took B's suggestion. I added the single quotes and all is well. Thanks a lot! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum