简体   繁体   中英

Bool value returns false when the function set it to true in Javascript

I am facing this problem, created a Audio Player in the document ready and a var for playing as false but every time function seems to be called correctly but not setting that variable to true: the code is this the function is called from a click listener.

var cartSongPlayer = new Audio();
var playingStatus = false;
var playingTrack;

function switchTrack(trackID, trackSource) {
    //logging basic information:
    console.log('Current Track ID ' + trackID)
    console.log('Current Track Source ' + trackSource)

    console.log('Previous Track ID ' + playingTrack)
    console.log('Playing Status ' + playingStatus)

    if (playingStatus) {
        console.log('song is playing')
        if (trackID == playingTrack) {
            playingStatus == false;
            console.log('pausing current song')
            cartSongPlayer.pause();

        } else {
            console.log('moved to new song')
            playingStatus == true;
            playingTrack = trackID
            cartSongPlayer.src = trackSource;
            cartSongPlayer.play();
        }
    //no song is playing
    } else {
        console.log('song is not playing')
        if (trackID == playingTrack) {
            playingStatus == true;
            console.log('resumed song')
            cartSongPlayer.play();

        } else {
            console.log('started new song')
            playingStatus == true;
            playingTrack = trackID
            cartSongPlayer.src = trackSource;
            cartSongPlayer.play();
        }
    }
}

You seem to be mistakingly using the == comparison operator, instead of the = assignment operator.

Change

playingStatus == true;

to

playingStatus = true;

You never change the value of playingStatus .

== is the equality operator, not the assignment operator (which is = ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM