[英]Is there a way to return a boolean value through setInterval() function?
我正在做一個視頻播放器項目,正在使用videojs以及videojs-ima進行廣告前貼片。
我終於找到了一種方法來判斷是否存在廣告; 但是,我試圖返回一個布爾值true
或false
以便告訴何時運行和執行某個代碼塊。
這是我的代碼,目前無法正常運行。
if (navigator.userAgent.match(/iPhone/i) ||
navigator.userAgent.match(/iPad/i) ||
navigator.userAgent.match(/Android/i)) {
let checkAd = document.querySelectorAll('.ima-ad-container');
let checkAdLoop = setInterval(() => {
for (let i=0; i < checkAd.length; i++) {
if (checkAd[i].style.display == 'none') {
console.log('Ad is NOT playing.');
return false;
} else {
console.log('Ad is playing.');
return true;
// do nothing
}
}
}, 1000);
player.on('touchend', () => {
if (player.paused() && checkAdLoop() == false) {
player.play();
} else if (player.currentTime > 0 && checkAdLoop() == false) {
player.pause();
}
});
$('.vjs-icon-placeholder').on('touchend', () => {
if (player.paused() && checkAdLoop() == false) {
player.play();
} else if (player.currentTime > 0 && checkAdLoop() == false) {
player.pause();
}
});
}
如果有人可以幫助我理解並向我解釋為什么這行不通,我將非常樂意。 我該如何解決呢?
我知道我要走了 提前致謝!
這是一個經典的XY問題。
試圖解決與總體情況無關的問題。
這是有效的代碼,以及我如何進行不同的處理。
if (navigator.userAgent.match(/iPhone/i) ||
navigator.userAgent.match(/iPad/i) ||
navigator.userAgent.match(/Android/i)) {
let checkAd = $('.ima-ad-container');
player.on('touchend', () => {
if (checkAd.css('display') != 'none') {
console.log('Ad is playing.');
// do nothing
} else if (checkAd.css('display') == 'none') {
console.log('Ad is NOT playing.');
if (player.paused()) {
player.play();
} else {
player.pause();
}
}
});
$('.vjs-icon-placeholder').on('touchend', () => {
if (checkAd.css('display') != 'none') {
console.log('Ad is playing.');
// do nothing
} else if (checkAd.css('display') == 'none') {
console.log('Ad is NOT playing.');
if (player.paused()) {
player.play();
} else {
player.pause();
}
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.