[英]JavaScript pause sound if false
我有一個獲取對象數組的異步 function,如果出現以下情況,我將soundOn
設置為true
:
soundOn = content.some(ele => ele.order_status === 'processing');
它部分起作用,例如,當上述為真時播放聲音 - 但是即使soundOn
評估為假,它也會繼續播放。 這是我的代碼的 rest。
function getOrders()
{
(async () => {
bgSound = new Audio("assets/sounds/spn.mp3");
soundOn = "";
const resp = await fetch(url+'/app/v1/post', {
//truncated
});
const content = await resp.json();
if(content.length > 0)
{
soundOn = content.some(ele => ele.order_status === 'processing');
}
(soundOn) ? setInterval(function () { bgSound.play() }, 5000) : bgSound.pause();
})();
setTimeout(getOrders, 30000);
}
內容數組看起來像這樣(它的一部分)。
[{"id":"1","order_status":"acknowledged", "type":"off"},
{"id":"1","order_status":"processing", "type":"off"},{"id":"1","order_status":"processing", "type":"off"}]
當應用程序正在運行時,這些狀態會更新為已確認,但聲音會繼續播放。 我不確定為什么會這樣,任何幫助將不勝感激。
添加條件可能會有所幫助:
(soundOn) ? setInterval(function () { if (soundOn) bgSound.play() }, 5000) : bgSound.pause();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.