No.1Deisgns
No.1Deisgns

Reputation: 29

Java Script Variation For Website

The website here has a music player http://www.numberonedesigns.com/ubermusic.com/ ... When hitting the next button with random highlighted it wont randomize correctly. It always goes back to the first song on the play-list then randomizes on the next.. Here's the variable code. I tried messing with it for a while but decided I needed some help

$next.click(function(){
        var num = 0;
        $.cookie('plate_time', 1);
        if($random.hasClass('active')){
            while(num == $audio.currentTrack){num = Math.floor(Math.random() * (opt.playlist.length));}
        }else{
            num = $audio.currentTrack - 1;
        }

        $audio.loadTrack(num);
    });

    $playlist.on('click', '.track', function(){
        $audio.loadTrack($(this).attr('rel'));
    });

    $prev.click(function(){
        var num = 0;
        $.cookie('plate_time', 0);
        if($random.hasClass('active')){
            while(num == $audio.currentTrack){num = Math.floor(Math.random() * (opt.playlist.length));}
        }else{
            num = $audio.currentTrack - 1;
        }

        $audio.loadTrack(num);
    });

Upvotes: 0

Views: 47

Answers (2)

TechnicalChaos
TechnicalChaos

Reputation: 452

Since you asked so nicely:

$next.click(function(){
    var num = Math.floor(Math.random() * (opt.playlist.length)); 
    //Set the random number here.
    $.cookie('plate_time', 1);
    if($random.hasClass('active')){
        while(num == $audio.currentTrack){num = Math.floor(Math.random() * (opt.playlist.length));} 
        // leaving it here JIC you get the same track.
    }else{
        num = $audio.currentTrack - 1; 
    // cos the ordering still works here if not on random. 
    }

    $audio.loadTrack(num);
});

$playlist.on('click', '.track', function(){
    $audio.loadTrack($(this).attr('rel'));
});

$prev.click(function(){
    var num = Math.floor(Math.random() * (opt.playlist.length));
    $.cookie('plate_time', 0);
    if($random.hasClass('active')){
        while(num == $audio.currentTrack){num = Math.floor(Math.random() * (opt.playlist.length));}
    }else{
        num = $audio.currentTrack - 1;
    }

    $audio.loadTrack(num);
});

Upvotes: 1

TechnicalChaos
TechnicalChaos

Reputation: 452

You're setting num to 0 at the start of the function. This means that when the current track is playlist[0] the function is triggered, but when the current track isn't playlist[0] but num gets set to 0, the random isn't called and playlist[0] is played again. The while check is failing you with the num set to 0 at the start.

Upvotes: 1

Related Questions