测试代码
<html>
<head>
<title>test cpm</title>
<script type="text/javascript" >
function createCookie(name,value,second) {
if (second)
{
var date = new Date();
date.setTime(date.getTime()+(second*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++)
{
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
} var turnAdIntervalSeconds = 2;//秒
var popUpCookieName = 'testPopUpSeconds';
function PopUp(targetUrl ,name,param){
var lastPopUpSeconds = readCookie(popUpCookieName);
if(!lastPopUpSeconds) lastPopUpSeconds = 0;
var currSeconds = new Date().getTime();
if(currSeconds-lastPopUpSeconds<turnAdIntervalSeconds*1000)
return false;
var casalef='width=800,height=600,toolbar=1,location=1,titlebar=1,menubar=1,scrollbars=1,resizable=1,directories=1,status=1';
var l = (screen.width - 800) / 2 ;
var t = (screen.height - 600) / 2 ;
var popWin = window.open('about:blank',currSeconds,casalef+',left='+l+',top='+t);
popWin.blur();
popWin.opener.focus();
popWin.location=targetUrl;
var popUpSeconds = new Date().getTime();
createCookie(popUpCookieName,popUpSeconds,60);
}
function clickPop(targetUrl,name,param){
document.onclick=function(){
var lastPopUpSeconds = readCookie(popUpCookieName);
if(!lastPopUpSeconds)lastPopUpSeconds = 0;
var currSeconds = new Date().getTime();
//if(currSeconds-lastPopUpSeconds<turnAdIntervalSeconds*1000)
// return false;
PopUp(url,name);
}
} var url = "http://www.baidu.com";
function startPopUp(){
try{
PopUp(url,'test');
}catch(e){
clickPop(url,'name');
}finally{
}
}
startPopUp();
</script>
</head> <body>
</body>
</html>我想的效果是:你点击该 页面,就会弹出窗口,但弹出窗口的时间间隔要2s
这样运行个页面,应该是每隔2s才会弹出窗口,但实际运行效果是,如果我在2s内点击该页面n次,2s后就会弹出n个窗口
如果把clickPop函数里的注释去掉,就能实现每2s弹出个窗口(你要点击该页面)这是怎么回事??
<html>
<head>
<title>test cpm</title>
<script type="text/javascript" >
function createCookie(name,value,second) {
if (second)
{
var date = new Date();
date.setTime(date.getTime()+(second*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++)
{
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
} var turnAdIntervalSeconds = 2;//秒
var popUpCookieName = 'testPopUpSeconds';
function PopUp(targetUrl ,name,param){
var lastPopUpSeconds = readCookie(popUpCookieName);
if(!lastPopUpSeconds) lastPopUpSeconds = 0;
var currSeconds = new Date().getTime();
if(currSeconds-lastPopUpSeconds<turnAdIntervalSeconds*1000)
return false;
var casalef='width=800,height=600,toolbar=1,location=1,titlebar=1,menubar=1,scrollbars=1,resizable=1,directories=1,status=1';
var l = (screen.width - 800) / 2 ;
var t = (screen.height - 600) / 2 ;
var popWin = window.open('about:blank',currSeconds,casalef+',left='+l+',top='+t);
popWin.blur();
popWin.opener.focus();
popWin.location=targetUrl;
var popUpSeconds = new Date().getTime();
createCookie(popUpCookieName,popUpSeconds,60);
}
function clickPop(targetUrl,name,param){
document.onclick=function(){
var lastPopUpSeconds = readCookie(popUpCookieName);
if(!lastPopUpSeconds)lastPopUpSeconds = 0;
var currSeconds = new Date().getTime();
//if(currSeconds-lastPopUpSeconds<turnAdIntervalSeconds*1000)
// return false;
PopUp(url,name);
}
} var url = "http://www.baidu.com";
function startPopUp(){
try{
PopUp(url,'test');
}catch(e){
clickPop(url,'name');
}finally{
}
}
startPopUp();
</script>
</head> <body>
</body>
</html>我想的效果是:你点击该 页面,就会弹出窗口,但弹出窗口的时间间隔要2s
这样运行个页面,应该是每隔2s才会弹出窗口,但实际运行效果是,如果我在2s内点击该页面n次,2s后就会弹出n个窗口
如果把clickPop函数里的注释去掉,就能实现每2s弹出个窗口(你要点击该页面)这是怎么回事??
return false;
这里如果return false以后你多余的点J就无效,所以有你说的那个效果如果不return false的话,你点几次就是几次
不过我在IE6下测试这个代码不点也自动弹出- -
if(currSeconds-lastPopUpSeconds>turnAdIntervalSeconds*1000){
return PopUp(url,name);
}
它的点击事件会先缓存起来,达到条件后再弹出
<head>
<title>test cpm</title>
<script type="text/javascript" >
function createCookie(name,value,second) {
if (second)
{
var date = new Date();
date.setTime(date.getTime()+(second*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++)
{
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
} var turnAdIntervalSeconds = 2;//秒
var popUpCookieName = 'testPopUpSeconds';
function PopUp(targetUrl ,name,param){
var lastPopUpSeconds = readCookie(popUpCookieName);
if(!lastPopUpSeconds) lastPopUpSeconds = 0;
var currSeconds = new Date().getTime();
if(currSeconds-lastPopUpSeconds<turnAdIntervalSeconds*1000)
return false;
var casalef='width=800,height=600,toolbar=1,location=1,titlebar=1,menubar=1,scrollbars=1,resizable=1,directories=1,status=1';
var l = (screen.width - 800) / 2 ;
var t = (screen.height - 600) / 2 ;
var popWin = window.open('about:blank',currSeconds,casalef+',left='+l+',top='+t);
popWin.blur();
popWin.opener.focus();
popWin.location=targetUrl;
var popUpSeconds = new Date().getTime();
createCookie(popUpCookieName,popUpSeconds,60);
}
function clickPop(targetUrl,name,param){
document.onclick=function(){
document.onclick = null;
setTimeout(function() {
var lastPopUpSeconds = readCookie(popUpCookieName);
if(!lastPopUpSeconds)lastPopUpSeconds = 0;
var currSeconds = new Date().getTime();
//if(currSeconds-lastPopUpSeconds<turnAdIntervalSeconds*1000)
// return false;
PopUp(url,name);
setTimeout(arguments.callee, turnAdIntervalSeconds * 1000);
}, turnAdIntervalSeconds * 1000);
}
} var url = "http://www.baidu.com";
function startPopUp(){
clickPop(url,'name');
}
startPopUp();
</script>
</head> <body>
</body>
</html>
PS: 劝楼主别搞这么恶心的AD弹出窗口脚本,真让人受不了....
不是缓存起来,是放入消息队列,等上一个handel执行完以后依次执行剩下的正因为如此,当执行到你的PopUp方法时
if(currSeconds-lastPopUpSeconds<turnAdIntervalSeconds*1000)
return false;条件已经变成满足(true)了,所以能继续执行而如果放在onclick的时候,就不会这种情况,因为当场就退出
怎么都不会出现
关键是点n次,还要过会(而不是马上),然后就n次依次弹出(我在PopUp函数里有if(currSeconds-lastPopUpSeconds <turnAdIntervalSeconds*1000)条件限制,怎么还会依次弹出,要弹就只弹一个,后面的就不应该不满足这个条件) 这是为什么
var lastPopUpSeconds = readCookie(popUpCookieName); //上一次弹出的时间的毫秒数
if(!lastPopUpSeconds) lastPopUpSeconds = 0;
var currSeconds = new Date().getTime();//现在的时间
if(currSeconds-lastPopUpSeconds<2*1000)现在时间减去上次的小于2s
return false;
每次都是弹完写,到一次弹出,这时间间隔应该会小于2s吧,按理不应该会出连续弹出的啊
qevent[事件1,事件2]
事件1开始执行,但是事件1执行了50秒,事件2从队列取出的时候初试时间就是50秒,和你预期的已经不同了