简体   繁体   English

当函数在Javascript中将布尔值设置为true时,布尔值将返回false

[英]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. 我遇到了这个问题,准备好在文档中创建一个Audio Player,并创建一个var来播放为false,但每次似乎都正确调用了函数,但未将该变量设置为true:此代码是从Click侦听器调用的函数。

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 . 您永远不会更改playingStatus的值。

== is the equality operator, not the assignment operator (which is = ). ==是相等运算符,不是赋值运算符( = )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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