Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery building of array not working from within first function being executed
    primarykey
    data
    text
    <p>The following issue is driving me nuts. I've tried all kinds of ways of solving it but I get nothing.</p> <p>The code is as follows</p> <pre><code>var item = []; function runCSStweaks() { var m=0; $('.jp-playlist li').each(function() { m++; var min1 = -1; var max1 = 1; var random1 = Math.floor(Math.random() * (max1 - min1 + 1)) + min1; item[m] = random1; $(this).css('-webkit-transform','rotate('+random1+'deg)') }); }; function actOnSongChange() { var m=0; $('.jp-playlist li').each(function() { m++; $(this).css('-webkit-transform','rotate('+item[m]+'deg)') .css('-webkit-transform','scale(1)') }); var min3 = -1; var max3 = 1; var random3 = Math.floor(Math.random() * (max3 - min3 + 1)) + min3; $('.jp-playlist .jp-playlist-current').css('-webkit-transform','rotate('+random3+'deg)') .css('-webkit-transform','scale(1.05)') }); </code></pre> <p>I'll walk you guys through it. Basically, I'm building a playlist using jPlayer. I've set it up so that, when jPlayer finished building the list, the <code>runCSStweaks()</code> code will be called in order to apply some small random CSS rotation to each item. (This was an earlier problem I had; could not get these CSS tweaks to be applied once the list was all done; so I had to solve it by placing the call to this variable at the end of the jPlayer list-building process.) Up to here, it works perfectly. All the items are randomly rotated as expected.</p> <p>Then, the idea is that, as each song finishes playing and goes on to the next, the second script, <code>actOnSongChange()</code>, is to be called. This script basically should apply another random rotation and a CSS scale to the current item playing, while returning all previously played items to their original scale.</p> <p>It seemed as if it would be pretty straightforward but I encountered the issue that I couldn't just set the CSS scale back to 1 without this resetting the original CSS rotation. I guess this is because they both work through CSS transform. So the only solution that comes to mind is to "save" all original rotations in order to call them back as well when the scale is brought back to 1.</p> <p>In order to do this, I'm creating an array of each random rotation amount when the first script is run and calling each individual array entry back inside the second script.</p> <p>Here is were I've hit a wall. I've tried everything and have come to the conclusion that the first script does not build the array at all. If I place a <code>console.log()</code> in the second script and call for <code>item</code> to have it dump the array's contents I get an empty array. If I try to place a <code>console.log()</code> within the first script to see what is going on it doesn't even print out. For this same reason, I was forced to place the <code>var item = []</code> line outside of the <code>runCSStweaks()</code> (otherwise, I got this error through <code>console.log(item)</code>: "Uncaught ReferenceError: item is not defined "). </p> <p>So, what is apparently obvious, this first function, <code>runCSStweaks()</code>, does the CSS tweaks alright, but that's about it. Anything else I place inside is not run and I don't know why.</p> <p>Am I perhaps missing something? Is there an error in my coding somewhere? Or does it have to do with the order in which jQuery calls and executes things?</p> <p>I'll continue trying to get through this code in the morning. But for now, my brain is about to build itself a bonfire.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload