/    Sign up×
Community /Pin to ProfileBookmark

Troubles with window.setTimeout()

Ok, I’ve been very bored lately so I figured I’d give a go at making a square slide around horizontally using HTML, CSS, and JavaScript. It works wonderfully, but I’m having some issues if I try to make my [I]window.setTimeout(‘document.getElementById(“square”).style.left = ‘ + hPosition + ‘px”‘, time);[/I] (where square is the name of the element) into [I]window.setTimeout(‘document.getElementById(“‘+elementID+'”).style.left = ‘ + hPosition + ‘px”‘, time);[/I].

Ultimately, I’d like to change it from [I]document.getElementById()[/I] to just element which I declare at the beginning of my function. Here’s the full function for reference (using +elementID+).

[CODE]function slideHorizontally(elementID, leftSide, rightSide) {
var element = document.getElementById(elementID);

if (runRightNext) {
var hPosition = rightSide;
var time = 0;
var currentPos = element.offsetLeft;
for (var i = 1; currentPos > leftSide; i++) {
hPosition-=2;
time=50*i;
window.setTimeout(‘document.getElementById(“‘+elementID+'”).style.left = ‘ + hPosition + ‘px”‘, time);
currentPos = hPosition;
}
runRightNext = false;
} else {
var hPosition = leftSide;
var time = 0;
var currentPos = element.offsetLeft;
for (var i = 1; currentPos < rightSide; i++) {
hPosition+=2;
time=50*i;
window.setTimeout(‘document.getElementById(“‘+elementID+'”).style.left = ‘ + hPosition + ‘px”‘, time);
currentPos = hPosition;
}
runRightNext = true;
}
}[/CODE]

elementID is any absolutely positioned element, leftSide is the starting point of the slide, and rightSide is the ending point. leftSide should be set to whatever your absolutely positioned element’s left attribute is set in your stylesheet.

When the function is called a first time it moves from left to right, and the second call moves from right to left, to move the element back to its initial position. If I don’t statically reference the element I want to move, it breaks, and FireBug gives me a “missing ; before statement” error.

So, what am I doing wrong? ?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@weegee101authorJan 09.2007 — Nobody has any clue about what's wrong with my code?
×

Success!

Help @weegee101 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.20,
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,
)...