繁体   English   中英

如何强制用户在新的弹窗停留一段时间?

[英]How to force users to stay at new popup window for certain time?

我想强制用户在允许他们关闭它之前在新标签页停留一段时间。

这是一个例子: https : //foom.egybest.co/当用户快速关闭弹出窗口时,它会阻止整个网站并通知用户禁用广告阻止。

 <img onclick="xxx()" src="https://icon-icons.com/icons2/1648/PNG/96/10094clownface_109962.png" > <script> function xxx() { var popUp = window.open('https://www.google.com/'); var timer = setInterval(function() { if(popUp.closed) { clearInterval(timer); alert('closed'); } }, 2000); if (popUp == null || typeof(popUp)=='undefined') { alert('AdBlock detect'); } else { popUp.focus(); } } </script>

如果您可以访问所有相关链接,则可以将postMessage添加到onbeforeunload

否则,您必须将持续时间逻辑与检查逻辑分开。 一种版本是将间隔设置为低以频繁检查并将开始时间传递给回调函数,其中可以计算经过的时间。

这是一个简短的例子:

 function xxx(link){ var tObject = { popUp: window.open(link), startTime: Date.now(), requiredWaitingTimeInMS: 2000, Timer: null }; //REM: No popup found if(!tObject.popUp){ alert('No popup detect') } else{ tObject.popUp.focus(); tObject.Timer = setInterval(function(object){ if(object){ //REM: If there is no popup or the popup is closed, we are done if(!object.popUp || object.popUp.closed){ //REM: Stopping timer and cleanup clearInterval(object.Timer); object = null; //REM: Output on fail alert('Not enough time') } //REM: Else if the popup is open and the waiting time passes 2000ms, we are done else if( Date.now() - object.startTime > object.requiredWaitingTimeInMS ){ //REM: Stopping timer and cleanup clearInterval(object.Timer); object = null; //REM: Output on success alert('Enough time') } } }.bind(this, tObject), 50) } }
 <img onclick="xxx(this.src)" src="https://icon-icons.com/icons2/1648/PNG/96/10094clownface_109962.png" >

暂无
暂无

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

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