【发布时间】:2017-05-31 16:17:11
【问题描述】:
以下代码工作正常,唯一的问题是点击事件排队,例如每次点击都会调用setTimeout,并且弹出窗口会出现多次。如何使弹出窗口仅在用户单击时出现,并确保每个弹出窗口之间的间隔为 4 秒
var showpopup = 1;
var check = true;
var seconds = 4000; // change the frequency here current frequency = 2 seconds
var url = "https://www.fb.com"; // change the url here
var strWindowFeatures = "toolbar=yes,scrollbars=yes,resizable=yes,top=0,left=0,width=" +screen.width
+ ",height=" +screen.height;
function popup (event)
{
event.stopPropagation();
if (showpopup === 1)
{
//if
(navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Windows
Phone|Opera Mini|IEMobile/i))
//{
if (check == true)
{
window.open(url , '_blank' , strWindowFeatures);
showpopup++;
check = false;
}
//}
}
else
{
setTimeout(checkValue, seconds);
}
}
function checkValue ()
{
showpopup = 1;
check = true;
}
window.onclick = popup(event);
【问题讨论】:
-
我原以为
window.onclick = popup(event);没有设置点击处理程序,因为您正在设置(使用空事件参数调用弹出窗口)的返回值来处理点击,而不是设置函数本身,如window.onclick = popup; -
对不起,我的错误发布了旧版本没有检查
标签: javascript dom-events mouseevent