What if you reversed the logic so that you have this:
for (let i = 0; i < arr.length; i++) {
//fade out img
//change src to new value in array
//fade in new value;
//put some kind of delay timer here so it doesn't happen too fast
}
Edit: I assumed above that one button click results in the array of images being shown. If you meant that each button click shows a different image, then the logic would be different.
hi!
Actually my machine is only randomized for the first quote only. I store the current quote number in the session storage and that way it is remembered between clicks.
If you have a codepen with your code , I can take a closer look at the log to see what is happening…
here’s my code (from the codepen I shared earlier)
$(document).ready(function () {
let currQnum = parseInt(sessionStorage.getItem('currentQuote'));
console.log("currQnum at start = "+ currQnum);
if (!sessionStorage.getItem('currentQuote')) {
console.log("Pretend first load and random quote needed");
currQnum = randFirstQuote(quoteArr);
sessionStorage.setItem('currentQuote', currQnum);
$("#author").text(quoteArr[currQnum].author);
$("#text").text('<span id="leftQ">"</span>'+quoteArr[currQnum].quote+'<span id="rightQ">"</span>');
$("a").attr('href',tweetIt(quoteArr[currQnum].quote,quoteArr[currQnum].author));
console.log(tweetIt(quoteArr[currQnum].quote,quoteArr[currQnum].author));
}
$("button").click(function() {
currQnum += 1;
if ( currQnum >= quoteArr.length ) {
currQnum = 0;
}
sessionStorage.setItem('currentQuote', currQnum);
$("a").attr('href',tweetIt(quoteArr[currQnum].quote,quoteArr[currQnum].author));
console.log(tweetIt(quoteArr[currQnum].quote,quoteArr[currQnum].author));
console.log("currentQuote exists and before increment is "+ sessionStorage.getItem('currentQuote'));
$("#centerBox").fadeOut(2000, "linear");
$("#quote-box").fadeOut(1800, "linear");
$("#author").fadeOut(1800, "linear", function() {
$("#author").text(quoteArr[sessionStorage.getItem('currentQuote')].author);
$("#author").fadeIn(1800, "linear");
});
$("#text").fadeOut(1800,"linear",function() {
$("#text").html( '<span id="leftQ">"</span>'+quoteArr[sessionStorage.getItem('currentQuote')].quote+'<span id="rightQ">"</span>');
$("#text").fadeIn(1800,"linear");
});
$("#centerBox").fadeIn(1600, "linear");
$("#quote-box").fadeIn(1400, "linear");
});
});
What I don’t understand is if you are at the last picture and the remainder is 0, isn’t that the zeroth index of the array and therefore the first and not the last picture? Just a little confused how this works.
If you want to cycle through 3 elements… The expression is n % 3 This results a cycle of 0 1 2 0 1 2.... This already lines up well with how array indexing, which counts from 0. So, if you decide to call your 1st element the 0th element, then everything is fine, which does not apply in this case. So, the solution will remain correct.
However, if you decided to count from 1, you will be off by one all the time. If you want to count from 1, then you need to introduce an offset.
Another question, I have another image in the slew of things that I don’t want to be jumbled into this function. Instead of calling $(‘img’)…is there a class I can call that set of images by?