/    Sign up×
Community /Pin to ProfileBookmark

using a function that requires parameters with setTimeout

Hi everybody,
I am trying to write some functions involving setTimeout, but I’m getting hung up on passing parameters to the function referenced by setTimeout. For example, I have a function RotatePictures that takes 3 parameters — a number, a string, and an array. The first two work fine (well, there might be an easier way to get them too, if anyone knows) but the array results in an error (the name is not recognized, has no properties):

[CODE]function rotatePictures(wait, billbd, picts) {
pictNum = pictNum+1; if (pictNum==picts.length) pictNum=0;
document.getElementById(billbd).src = picts[pictNum].name;
setTimeout(‘rotateBoard(‘+wait+’, “‘+billbd+'”, picts)’, wait);
}[/CODE]

At one point I tried to make a custom object with rotatePictures as a method, so that the call to setTimeout was

setTimeout(‘this.rotatePictures();’);

However, I got an error that this.rotatePictures was not recognized.

I am guessing that the reason for these errors is that when the thread calls rotatePictures, it is out of the scope of ‘this’ or picts, but I don’t know what to do about it.

thanks for your help! Karen.

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@mrhooMar 17.2006 — Make up your string first, then call setTimeout:

[CODE]function rotatePictures(wait, billbd, picts) {
pictNum = pictNum+1;
if (pictNum==picts.length) pictNum=0;
document.getElementById(billbd).src = picts[pictNum].name;
var str='rotateBoard('+wait+', "'+billbd+'", picts)';
setTimeout(str, wait);
}
[/CODE]


if it doesn't work:

look at the string to see why.

setTimeout works just like eval, but eval gives you better errors:

try eval(str) just for debugging if it gets mean.

Or pass the code without the timeout and make sure that works
Copy linkTweet thisAlerts:
@James_GatkaMar 17.2006 — Create the image array like this:

[CODE]<html>
<head>
<script type="text/javascript">

var imgArray = [];
imgArray['picts'] = ["one.jpg","two.jpg","three.jpg"];

function rotateBoard(nPix){

alert(nPix);
}

function rotatePictures(){

setTimeout("rotateBoard(imgArray['picts'])", 1000);
}

</script>
</head>
<body>

<input type="button" value="Test" onclick="rotatePictures()">


</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@griff777Mar 17.2006 — Hi everybody,

I am trying to write some functions involving setTimeout, but I'm getting hung up on passing parameters to the function referenced by setTimeout. For example, I have a function RotatePictures that takes 3 parameters -- a number, a string, and an array. The first two work fine (well, there might be an easier way to get them too, if anyone knows) but the array results in an error (the name is not recognized, has no properties):

[CODE]function rotatePictures(wait, billbd, picts) {
pictNum = pictNum+1; if (pictNum==picts.length) pictNum=0;
document.getElementById(billbd).src = picts[pictNum].name;
setTimeout('rotateBoard('+wait+', "'+billbd+'", picts)', wait);
}[/CODE]


At one point I tried to make a custom object with rotatePictures as a method, so that the call to setTimeout was

setTimeout('this.rotatePictures();');

However, I got an error that this.rotatePictures was not recognized.

I am guessing that the reason for these errors is that when the thread calls rotatePictures, it is out of the scope of 'this' or picts, but I don't know what to do about it.

thanks for your help! Karen.[/QUOTE]


I noticed, the "function" name is "rotatePictures", but the setTimeout call is to "rotateBoard".

I am not sure, but usually the "setTimeout" points back or "recursively" to the same function that originally set it.

Thus, a timer'd function that is always in the loop.

Not sure that helps...
Copy linkTweet thisAlerts:
@Karen_NelsonauthorMar 22.2006 — Thanks everybody for your advice. In the end I did get it to work by messing around with various permutations of quotation marks, and fixing the mixup between rotateBoard and rotatePictures. The suggestion to use eval is a good one!

-Karen.
Copy linkTweet thisAlerts:
@phpnoviceMar 22.2006 — This:
<i>
</i>setTimeout('rotateBoard('+wait+', "'+billbd+'", picts)', wait);

is more simply coded as follows (no slashes required):
<i>
</i>setTimeout('rotateBoard('+wait+', "'+billbd+'", picts)', wait);

As for passing an array in setTimeout as an argument, in your case you can code that this way:
<i>
</i>setTimeout('rotateBoard('+wait+', "'+billbd+'", ["'+pics.join('","')+'"])', wait);
Copy linkTweet thisAlerts:
@gphMar 22.2006 — To add, you can pass objects like this
<i>
</i>setTimeout(function(){rotateBoard(wait,billbd,picts)},wait);
Copy linkTweet thisAlerts:
@phpnoviceMar 22.2006 — To add, you can pass objects like this
<i>
</i>setTimeout(function(){rotateBoard(wait,billbd,picts)},wait);
[/QUOTE]

Just curious, but have you actually used that method successfully? I ask, because, just looking at it, it seems like it could not work -- i.e., scope being an issue. I could try it myself, but I though I'd just ask you. ?
Copy linkTweet thisAlerts:
@gphMar 22.2006 — Yes I use it alot, it's a lazy persons approach to setTimeout ?
<i>
</i>function foo(){
var a=[window.foo];
setTimeout(function(){alert(a[0])},100);
}
foo()
Copy linkTweet thisAlerts:
@shrageApr 06.2006 — i have designed a person object which has functions

this.GetFullName = function()

{

return this.FirstName + " " + this.LastName;

}

now i want to call the GetFullName() function of this instance using the window.setTimeOut function

this.AlertFullName = function(wait)

{

window.setTimeOut(function(){alert(this.GetFullName())},wait);

}

But it dosen't work, how do i go about it?
Copy linkTweet thisAlerts:
@gphApr 06.2006 — setTime[COLOR=Red]o[/COLOR]ut
×

Success!

Help @Karen_Nelson spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...