在网上找了一些资料,制作了一个计时器,通过鼠标单击控制计时器的开始、暂停和复位,程序运行时基本正常,但如果鼠标双击时,计时器就失控了,无法停止和复位,计时器只能不停的走,请问如何解决此问题。可以考虑屏蔽双击变成单击吗?如何实现?编写的相关代码如下:
<BODY onmousedown=stop(event) onmouseup=begin(event) onload=rndCube()>function stop(evt)
{
if (runing==1 && evt.button==1)
clearTimeout(timeint);
}
////////
function begin(evt)
{
if (evt.button==1)
{
if (runing==0){
then=new Date();
runing=1;
show();
}
else if (runing==1)
{
runing=2;
}
else 
{
ShowDatas(toSeconds(nowSeconds))
document.getElementById("watch").innerHTML="00:00.00";
runing=0;

}
}
}

解决方案 »

  1.   

    <BODY onmousedown=stop(event) onmouseup=begin(event) onload=rndCube()>
    你试下不要用onmousedown ,onmouseup
    改为onclick,再里面写
      

  2.   

    <BODY onclick="stop(event)" onload=rndCube()>function stop(evt)
    {
    if (runing==1 && evt.button==1)
       clearTimeout(timeint);
    else
       begin(evt)
    }
      

  3.   


    onclick=function(){
    //TODO
    return false;
    }
      

  4.   

    ondblclick = function(){onclick...}
      

  5.   

    <BODY ondbclick="return false;" onclick="stop(event)" onload=rndCube()>
    这样双击就无效了
      

  6.   


    改成这样的话程序单击或双击都无反应;
     
    改成<BODY ondblclick="return false;" onmousedown=stop(event) onmouseup=begin(event)onload=rndCube()>
    时,程序跟原来的一样,双击时无法停止;
     
    好像加了ondblclick后,双击始终无法屏蔽掉。
      

  7.   

    屏蔽双击有什么困难?你不监听dblclick事件不就行了?不知道你的是什么情形我觉得屏蔽双击里面附带的那个单击才是麻烦事。!!
      

  8.   

    你可以考虑设一个变量lastClickTime保存最后一次click的时间,然后在每次click触发的时候判断当前时间和lastClickTime的差值,如果差值小于200ms就说明两次click太快,认为是双击而跳过处理,如果差值大于等于200ms就执行你要的处理,并更新lastClickTime为当前时间。
      

  9.   

    这是计时器的源代码,请大家帮忙解决,谢谢:<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><HTML><HEAD><TITLE>计时器</TITLE>
    <META http-equiv=Content-Type content="text/html; charset=gb2312">
    <STYLE>TD {
    FONT: 9pt verdana
    }
    INPUT {
    FONT: 9pt verdana
    }
    DIV {
    FONT: 9pt verdana
    }
    TH {
    FONT: 9pt verdana
    }
    P {
    LINE-HEIGHT: 60%
    }
    </STYLE><META content="MSHTML 6.00.2900.5726" name=GENERATOR></HEAD>
    <BODY onmousedown=stop(event) onmouseup=begin(event) onload=rndCube()>
    <SCRIPT type=text/javascript>
    var Max=25;
    var bestAve;
    var then;
    var nowDec;
    var nowBest;
    var nowWorst;
    var runing=0;
    var timeint;
    var i=0;
    var totalsec;
    var sec;
    var min;
    var subsec;
    var secdis;
    var mindis;
    var subsecdis;
    var datas=new Array()
    var nowSecondes;function getAv(st,ed)
    {
    for(i=st;i<=ed;i++)
    {
    datas.join(" ")

    }
    }function stop(evt)
    {
    if (runing==1 && onclick==1)
    clearTimeout(timeint);
    }
    function toSeconds(dec)
    { var Ms,sec
    var tmpDec=dec.split(":")
    if(tmpDec[0]*10/10>0)
    {
    Ms=tmpDec[0]*60
    return(Ms+tmpDec[1]*10/10)
    }else{
    return(tmpDec[1]*10/10)
    }

    }function begin(evt)
    {
    if (onclick==1) {
    if (runing==0){
    then=new Date();
    runing=1;
    show();
    }
    else if (runing==1)
    {
    runing=2;
    }
    else 
    {
    ShowDatas(toSeconds(nowSeconds))
    document.getElementById("watch").innerHTML="00:00.00";
    runing=0;//0

    }
    }
    }
    var a=0;
    function ShowDatas(dec)
    {
    if(dec==0){return false}
    var newDec=get2(dec)
    datas.push(newDec)


    SortDatas()
    ShowDetails()
    spanBest.innerHTML="<b style=color:red>"+sec2minsec(nowBest)+"</b>"
    spanWorst.innerHTML="<b>"+sec2minsec(nowWorst)+"</b>"
    var tmp1=0;
    for(i=0;i<datas.length;i++)
    {
    tmp1+=datas[i]
    }

    spanAv.innerHTML="<b>"+sec2minsec(get2(tmp1/datas.length))+"</b>"
     
    }
    var bestAve=new Array();
    function sec2minsec(n)
    {       if (n>60)
            {
            var tmpmins=Math.floor(n/60);
    var tmpseconds=get2(n-tmpmins*60);
    var tmpstring
    if (tmpseconds<10)
    {  tmpstring=tmpmins+":0"+tmpseconds;
    }
       else  tmpstring=tmpmins+":"+tmpseconds;
     return tmpstring
     }
     else return n;
    }
    function ShowDetails()
    {
    if(datas.length>10){a=datas.length -10}
    var tmpTd="<table cellspacing=1 bgcolor=olive><tr align=center bgcolor=EEEEEE><th width=50>1<th width=50>2<th width=50>3<th width=50>4<th width=50>"
    tmpTd+="5<td width=50>6<th width=50>7<th width=50>8<th width=50>9<th width=50>10</tr><tr bgcolor=white>";
    for(i=a;i<(10+a);i++)
    {
    bestAve[i-a]=datas[i];
    tmpTd+="<td align=center>"
    if(datas[i]==undefined){tmpTd+="-"}
    else
    {
    if(datas[i]==nowBest || datas[i]==nowWorst)
    {    tmpTd+=sec2minsec(datas[i])+"*"
    }else
    {  
      tmpTd+=sec2minsec(datas[i]); 
    }
    }
    tmpTd+="</td>"
    }
    tmpTd+="</tr></table>"
    //alert(datas.join(","));
    div1.innerHTML="";
    div1.innerHTML=tmpTd;
    rndCube();
    document.focus();

    }
    function show()
    { var now=new Date();
    diff=now.getTime()-then.getTime();
    totalsec=Math.floor(diff/1000);
    sec=totalsec%60;
    if(sec<10)
    {
    secdis="0"+sec;
    }
    else
    {
    secdis=sec;
    }
    min=(totalsec-sec)/60;

    if (min<10)
    {
    mindis="0"+min;
    }
    else
    {
    mindis=min;
    }

    subsec=Math.floor((diff%1000)/10);

    if (subsec<10)
    {
    subsecdis="0"+subsec;
    }
    else 
    {
    subsecdis=subsec;
    }
    nowSeconds=mindis+":"+secdis+"."+subsecdis;
    document.getElementById("watch").innerHTML=nowSeconds;
    timeint=setTimeout("show()",50)
    }
    function get2(dec) 
    {
    return Math.round(dec*100)/100
    }
     
    function SortDatas()
    {
    var tmpArr=new Array()
    for(i=0;i<datas.length;i++)
    {
    tmpArr[i]=datas[i]
    }
    tmpArr.sort(function(a,b){ return a-b;})
    nowBest=tmpArr[0]
    nowWorst=tmpArr[tmpArr.length-1]
     ShowBestAv()
    }function mycls()
    {  
    clearTimeout(timeint);
    nowSeconds="00:00:00";
    runing=0;
    datas=new Array(); 
    ShowDatas(0);
    ShowDetails();
    document.getElementById("watch").innerHTML="00:00.00";
    btn1.focus();
    }function ShowBestAv()
    {
    var av=0;
    bestAve.sort();
    if(bestAve[11]==undefined){return false}
    for(i=1;i<bestAve.length-1;i++)
    {
    av+=bestAve[i];
    }
    spanBestAv.innerHTML=get2(av/10);
    }
    </SCRIPT><SCRIPT>
    function cancel()
    {   clearTimeout(timeint); nowSeconds="00:00:00";

    runing=0;
    btn1.focus();
    rndCube();
    document.getElementById("watch").innerHTML="00:00.00";

    }
    function rndCube()
    {
    var move="";
    var rndMove=new Array("R","L","F","B","U","D")
    var add=0;
    var tmpRnd;
    var arr=new Array(); while(true)
    {
    if(add>=Max){break}
    //tmpRnd=Math.round(Math.random()*5)
    if(tmpRnd==arr[arr.length -1])
    { tmpRnd=Math.floor(Math.random()*6);
    }
    else
    { arr.push(tmpRnd)
    add++
    }

    }
    for(i=0;i<arr.length;i++)
    {
    var tmp=Math.floor(Math.random()*5)
    if(tmp==4)
    { move+=rndMove[arr[i]]+"2";}
    else if(tmp==2 || tmp==3)
    { move+=rndMove[arr[i]]+"'";}
                else 
                { move+=rndMove[arr[i]];}
    move+=" ";
    }


    rndDiv.innerHTML=move;
    }</SCRIPT>
    <style>
    #top{background:#ffffff;line-height:14px;color:#666666;font-size:12px;position:absolute;top:2px;right:6pt;border-bottom:1px solid #000099;padding-top:3px}
    #top a{font-size:12px;text-decoration:none;padding:2px 2px 1px 2px;}
    #top a:link,#top a:visited{color:#000099}
    #top a:active,#top a:hover{color:#ff0000}
    #top span{padding:2px 2px 1px 2px;line-height:14px}
    /* .current_dir{color:#FFFFFF} */
    #top .current_dir{color:#FFFFFF;background:#cccccc;border:1px solid #666666;border-bottom:0}
    #top .current_dir a:link,#top current_dir a:visited{color:#ffffff}
    </style><CENTER><h1>计时器</h1>
    <br><br>
    <DIV id=watch 
    style="FONT-WEIGHT: bold; FONT-SIZE: 120pt; FILTER: shadraw(x=1,y=1,color=black); WIDTH: 359px; COLOR: navy; FONT-FAMILY: Arial; POSITION: relative; HEIGHT: 80px; TEXT-ALIGN: center; left: 0px; top: 0px;">00:00.00</DIV>
    <P><FONT size=2><strong style="color: red">
    <br><br><br><br><br>
    使用说明:</strong>用&nbsp;<FONT 
    color=#000080><B>按钮/鼠标&nbsp;</B></FONT><B>开始</B>、<B>停止</B>、<B>复位</B>(同时记录成绩)</FONT></P>
    <P><B><FONT color=#ff0000 size=2>注意:</FONT></B><FONT 
    size=2>请先最大化窗口使鼠标在网页范围内,且<FONT color=#ff0000 size=2>不能双击,否则计时器会失效。</FONT></font></P>
    <DIV id=rndDiv style="FONT: bold 12pt Arial"></DIV> <INPUT onclick=mycls(); type=button value=清除所有记录><INPUT onclick=cancel();div1.focus() type=button value=不记录此次时间> 
     <INPUT id=btn1 style="WIDTH: 0px; HEIGHT: 0px" onclick=document.focus type=button> 
      
    <DIV id=div1>
    <TABLE cellSpacing=1 bgColor=olive>
      <TBODY>
      <TR align=middle bgColor=#eeeeee>
        <TD width=50>1 
        <TD width=50>2 
        <TD width=50>3 
        <TD width=50>4 
        <TD width=50>5 
        <TD width=50>6 
        <TD width=50>7 
        <TD width=50>8 
        <TD width=50>9  
        <TD width=50>10 </TD>
      <TR align=middle bgColor=white>
        <TD width=50>- 
        <TD width=50>- 
        <TD width=50>- 
        <TD width=50>- 
        <TD width=50>- 
        <TD width=50>- 
        <TD width=50>- 
        <TD width=50>- 
        <TD width=50>- 
     
        <TD width=50>- </TD></TR></TBODY></TABLE></DIV></CENTER>
    <TABLE align=center>
      <TBODY>
      <TR>
        <TD>最快:</TD>
        <TD><SPAN id=spanBest></SPAN></TD></TR>
      <TR>
        <TD>最慢:</TD>
        <TD><SPAN id=spanWorst></SPAN></TD></TR>
      <TR>
        <TD>平均:</TD>
        <TD><SPAN id=spanAv></SPAN></TD></TR>
      <TR>
        </TBODY></TABLE></BODY></HTML>
      

  10.   

    上面代码有误,更正为:<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><HTML><HEAD><TITLE>计时器</TITLE>
    <META http-equiv=Content-Type content="text/html; charset=gb2312">
    <STYLE>TD {
    FONT: 9pt verdana
    }
    INPUT {
    FONT: 9pt verdana
    }
    DIV {
    FONT: 9pt verdana
    }
    TH {
    FONT: 9pt verdana
    }
    P {
    LINE-HEIGHT: 60%
    }
    </STYLE><META content="MSHTML 6.00.2900.5726" name=GENERATOR></HEAD>
    <BODY  onmousedown=stop(event) onmouseup=begin(event) onload=rndCube()>
    <SCRIPT type=text/javascript>
    var Max=25;
    var bestAve;
    var then;
    var nowDec;
    var nowBest;
    var nowWorst;
    var runing=0;
    var timeint;
    var i=0;
    var totalsec;
    var sec;
    var min;
    var subsec;
    var secdis;
    var mindis;
    var subsecdis;
    var datas=new Array()
    var nowSecondes;function getAv(st,ed)
    {
    for(i=st;i<=ed;i++)
    {
    datas.join(" ")

    }
    }function stop(evt)
    {
    if (runing==1 && evt.button==1)
    clearTimeout(timeint);
    }
    function toSeconds(dec)
    { var Ms,sec
    var tmpDec=dec.split(":")
    if(tmpDec[0]*10/10>0)
    {
    Ms=tmpDec[0]*60
    return(Ms+tmpDec[1]*10/10)
    }else{
    return(tmpDec[1]*10/10)
    }

    }function begin(evt)
    {
    if (evt.button==1) {
    if (runing==0){
    then=new Date();
    runing=1;
    show();
    }
    else if (runing==1)
    {
    runing=2;
    }
    else 
    {
    ShowDatas(toSeconds(nowSeconds))
    document.getElementById("watch").innerHTML="00:00.00";
    runing=0;//0

    }
    }
    }
    var a=0;
    function ShowDatas(dec)
    {
    if(dec==0){return false}
    var newDec=get2(dec)
    datas.push(newDec)

    SortDatas() ShowDetails()
    spanBest.innerHTML="<b style=color:red>"+sec2minsec(nowBest)+"</b>"
    spanWorst.innerHTML="<b>"+sec2minsec(nowWorst)+"</b>"
    var tmp1=0;
    for(i=0;i<datas.length;i++)
    {
    tmp1+=datas[i]
    }

    spanAv.innerHTML="<b>"+sec2minsec(get2(tmp1/datas.length))+"</b>"
     
    }
    var bestAve=new Array();
    function sec2minsec(n)
    {       if (n>60)
            {
            var tmpmins=Math.floor(n/60);
    var tmpseconds=get2(n-tmpmins*60);
    var tmpstring
    if (tmpseconds<10)
    {  tmpstring=tmpmins+":0"+tmpseconds;
    }
       else  tmpstring=tmpmins+":"+tmpseconds;
     return tmpstring
     }
     else return n;
    }
    function ShowDetails()
    {
    if(datas.length>10){a=datas.length -10}
    var tmpTd="<table cellspacing=1 bgcolor=olive><tr align=center bgcolor=EEEEEE><th width=50>1<th width=50>2<th width=50>3<th width=50>4<th width=50>"
    tmpTd+="5<td width=50>6<th width=50>7<th width=50>8<th width=50>9<th width=50>10</tr><tr bgcolor=white>";
    for(i=a;i<(10+a);i++)//12
    {
    bestAve[i-a]=datas[i];
    tmpTd+="<td align=center>"
    if(datas[i]==undefined){tmpTd+="-"}
    else
    {
    if(datas[i]==nowBest || datas[i]==nowWorst)
    {    tmpTd+=sec2minsec(datas[i])+"*"
    }else
    {  
      tmpTd+=sec2minsec(datas[i]); 
    }
    }
    tmpTd+="</td>"
    }
    tmpTd+="</tr></table>"
    //alert(datas.join(","));
    div1.innerHTML="";
    div1.innerHTML=tmpTd;
    rndCube();
    document.focus();

    }
    function show()
    { var now=new Date();
    diff=now.getTime()-then.getTime();
    totalsec=Math.floor(diff/1000);
    sec=totalsec%60;
    if(sec<10)
    {
    secdis="0"+sec;
    }
    else
    {
    secdis=sec;
    }
    min=(totalsec-sec)/60;

    if (min<10)
    {
    mindis="0"+min;
    }
    else
    {
    mindis=min;
    }

    subsec=Math.floor((diff%1000)/10);

    if (subsec<10)
    {
    subsecdis="0"+subsec;
    }
    else 
    {
    subsecdis=subsec;
    }
    nowSeconds=mindis+":"+secdis+"."+subsecdis;
    document.getElementById("watch").innerHTML=nowSeconds;
    timeint=setTimeout("show()",50)
    }
    function get2(dec) 
    {
    return Math.round(dec*100)/100
    }
     
    function SortDatas()
    {
    var tmpArr=new Array()
    for(i=0;i<datas.length;i++)
    {
    tmpArr[i]=datas[i]
    }
    tmpArr.sort(function(a,b){ return a-b;})
    nowBest=tmpArr[0]
    nowWorst=tmpArr[tmpArr.length-1]
     ShowBestAv()
    }function mycls()
    {  
    clearTimeout(timeint);
    nowSeconds="00:00:00";
    runing=0;
    datas=new Array(); 
    ShowDatas(0);
    ShowDetails();
    document.getElementById("watch").innerHTML="00:00.00";
    btn1.focus();
    }function ShowBestAv()
    {
    var av=0;
    bestAve.sort();
    if(bestAve[11]==undefined){return false}
    for(i=1;i<bestAve.length-1;i++)
    {
    av+=bestAve[i];
    }
    spanBestAv.innerHTML=get2(av/10);
    //alert(bestAve)}
    </SCRIPT><SCRIPT>
    function cancel()
    {   clearTimeout(timeint); nowSeconds="00:00:00";

    runing=0;
    btn1.focus();
    rndCube();
    document.getElementById("watch").innerHTML="00:00.00";

    }
    function rndCube()
    {
    var move="";
    var rndMove=new Array("R","L","F","B","U","D")
    var add=0;
    var tmpRnd;
    var arr=new Array(); while(true)
    {
    if(add>=Max){break}
    if(tmpRnd==arr[arr.length -1])
    { tmpRnd=Math.floor(Math.random()*6);
    }
    else
    { arr.push(tmpRnd)
    add++
    }

    }
    for(i=0;i<arr.length;i++)
    {
    var tmp=Math.floor(Math.random()*5)
    if(tmp==4)
    { move+=rndMove[arr[i]]+"2";}
    else if(tmp==2 || tmp==3)
    { move+=rndMove[arr[i]]+"'";}
                else 
                { move+=rndMove[arr[i]];}
    move+=" ";
    }


    rndDiv.innerHTML=move;
    }</SCRIPT>
    <style>
    #top{background:#ffffff;line-height:14px;color:#666666;font-size:12px;position:absolute;top:2px;right:6pt;border-bottom:1px solid #000099;padding-top:3px}
    #top a{font-size:12px;text-decoration:none;padding:2px 2px 1px 2px;}
    #top a:link,#top a:visited{color:#000099}
    #top a:active,#top a:hover{color:#ff0000}
    #top span{padding:2px 2px 1px 2px;line-height:14px}
    /* .current_dir{color:#FFFFFF} */
    #top .current_dir{color:#FFFFFF;background:#cccccc;border:1px solid #666666;border-bottom:0}
    #top .current_dir a:link,#top current_dir a:visited{color:#ffffff}
    </style><CENTER><h1>计时器</h1>
    <br><br>
    <DIV id=watch 
    style="FONT-WEIGHT: bold; FONT-SIZE: 120pt; FILTER: shadraw(x=1,y=1,color=black); WIDTH: 359px; COLOR: navy; FONT-FAMILY: Arial; POSITION: relative; HEIGHT: 80px; TEXT-ALIGN: center; left: 0px; top: 0px;">00:00.00</DIV>
    <P><FONT size=2><strong style="color: red">
    <br><br><br><br><br>
    使用说明:</strong>用&nbsp;<FONT 
    color=#000080><B>按钮/鼠标&nbsp;</B></FONT><B>开始</B>、<B>停止</B>、<B>复位</B>(同时记录成绩)</FONT></P>
    <P><B><FONT color=#ff0000 size=2>注意:</FONT></B><FONT 
    size=2>请先最大化窗口使鼠标在网页范围内,且<FONT color=#ff0000 size=2>不能双击,否则计时器会失效。</FONT></font></P>
    <DIV id=rndDiv style="FONT: bold 12pt Arial"></DIV> <INPUT onclick=mycls(); type=button value=清除所有记录><INPUT onclick=cancel();div1.focus() type=button value=不记录此次时间> 
     <INPUT id=btn1 style="WIDTH: 0px; HEIGHT: 0px" onclick=document.focus type=button> 
      
    <DIV id=div1>
    <TABLE cellSpacing=1 bgColor=olive>
      <TBODY>
      <TR align=middle bgColor=#eeeeee>
        <TD width=50>1 
        <TD width=50>2 
        <TD width=50>3 
        <TD width=50>4 
        <TD width=50>5 
        <TD width=50>6 
        <TD width=50>7 
        <TD width=50>8 
        <TD width=50>9  
        <TD width=50>10 </TD>
      <TR align=middle bgColor=white>
        <TD width=50>- 
        <TD width=50>- 
        <TD width=50>- 
        <TD width=50>- 
        <TD width=50>- 
        <TD width=50>- 
        <TD width=50>- 
        <TD width=50>- 
        <TD width=50>- 
     
        <TD width=50>- </TD></TR></TBODY></TABLE></DIV></CENTER>
    <TABLE align=center>
      <TBODY>
      <TR>
        <TD>最快:</TD>
        <TD><SPAN id=spanBest></SPAN></TD></TR>
      <TR>
        <TD>最慢:</TD>
        <TD><SPAN id=spanWorst></SPAN></TD></TR>
      <TR>
        <TD>平均:</TD>
        <TD><SPAN id=spanAv></SPAN></TD></TR>
      <TR>
        </TBODY></TABLE></BODY></HTML>