/    Sign up×
Community /Pin to ProfileBookmark

JavaScript — decrement problem

Hi everyone,

I’m having a little trouble with a JavaScript script. It’s a picture slide show.

Basically, when I click ‘Previous’ to call the ‘moveToPreviousSlide’ function, on the first call, it increments instead of decrements.

Say I’m currently viewing image 3, when I then click ‘previous’, it keeps going to image 4 instead, and then goes back down to 3, and then 2, instead of going straight to 2.

Here is what I’ve made so far:

[CODE]<script type=”text/javascript”>
var index = 0;
var titles = [1,2,3,4,5,6];

function moveToNextSlide()
{
if(index > 5)
{
index = 0;
}
var slideName = “images/slideshow/template” + titles[index++] + “.jpg”;
var img1 = document.getElementById(“img1”);
img1.src = slideName;
}

function moveToPreviousSlide()//on first call still increments by 1
{

var slideName = “images/slideshow/template” + titles[index–] + “.jpg”;
var img1 = document.getElementById(“img1”);
img1.src = slideName;
}

moveToNextSlide();
</script>[/CODE]

I’ve tried switching from post and pre increment / decrement, and a combination of the two, but this isn’t correcting it.

If anyone can spot where the problem is, it would be very much appreciated.

many thanks!

?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@scragarDec 01.2008 — use --image so it decreases the number first(as opposed to after), you'll have to mess about with your logic a bit as well.
Copy linkTweet thisAlerts:
@astupidnameDec 01.2008 — I would not recommend doing this: titles[index++] , should do the incrementing or decrementing before using its value.

And you can do it all in one function. I don't see the need for the titles array of numbers either, the way it is. Assuming your template[number].jpg pics start at number 1 and the last pics number is 6, the script could be like this:

[CODE]<script type="text/javascript>
var index = 1;
//pass 'plus' in the function call from button for next pic, example: onclick="chgSlide('plus');" or pass 'minus' in the previous pic button
function chgSlide(plusORminus){
if (plusORminus === 'plus') {
index = (index < 6) ? index + 1 : 1; //if index is less than 6 it will add 1 to itself, if it is already 6 or greater index will become 1
}
if (plusORminus === 'minus') {
index = (index > 1) ? index - 1 : 6; //if index is greater than 1 it will subtract 1 from itself, if it is already 1 it will become 6
}
var slideName = "images/slideshow/template" + index + ".jpg"; //will display template1.jpg through template6.jpg
document.getElementById("img1").src = slideName;
}
</script>[/CODE]
Copy linkTweet thisAlerts:
@Web_Design_101authorDec 02.2008 — Just want to say Thank You to both of you.

astupidname - Thank you for giving me such a great explanation, I've been trying to fix this code for ages, you wouldn't believe some of the 'patching' pieces of code I tried to handle exceptions.

Really appreciated.

?
Copy linkTweet thisAlerts:
@astupidnameDec 02.2008 — You're welcome, glad I could help!

you wouldn't believe some of the 'patching' pieces of code I tried[/quote]
Oh yes I would! I am still learning, myself. I still look back on things I did a few months or even a few weeks ago, and say, "boy I sure could have done that better". Learning is an ongoing process, and stupidity is an integral part of it. (No negative implication intended, just saying we all start somewhere - usually with the same level of stupidity towards what we are learning).

Good luck! :-)
×

Success!

Help @Web_Design_101 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.18,
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,
)...