我正面临这个问题,在文档准备就绪时创建了一个音频播放器,并且将var作为假播放,但是每次函数似乎都被正确调用但没有将该变量设置为true: 代码是这个函数是从点击监听器调用的。
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();
}
}
}
答案 0 :(得分:3)
您似乎错误地使用了==
比较运算符,而不是=
赋值运算符。
更改
playingStatus == true;
到
playingStatus = true;
答案 1 :(得分:2)
您永远不会更改playingStatus
的价值。
==
是等于运算符,而不是赋值运算符(=
)。