【问题标题】:starting countdown timer on right mouse click in javascript在javascript中单击鼠标右键开始倒计时
【发布时间】:2014-01-29 20:58:46
【问题描述】:

我这里有代码,在 JavaScript 中提供了简单的倒数计时器

var Timer;
var TotalSec;

function CreateTimer(Timer ID, Time){
    Timer = document.getElementByID(Timer ID);
    TotalSec = Time;
    UpdateTimer() window.setTimeout("Tick()", 1000);
}

function Tick() { 
    if (TotalSeconds <= 0) { 
        alert(message)
         return; 
        }
 TotalSeconds -= 1; 
 UpdateTimer() window.setTimeout("Tick()", 1000); 
}

function UpdateTimer() {
 Timer.innerHTML = TotalSeconds;
}

我也有代码在右键单击时显示一条消息

var message="Right click? You are using it wrong!  Thank you for your understanding.";

function clickIE4(){
if (event.button==2){
alert(message);
return false;
}
}

function clickNS4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
alert(message);
return false;
}
}
}

if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
document.onmousedown=clickIE4;
}

document.oncontextmenu=new Function("alert(message);return false")

好的,现在,我想做的是,当有人在网站上单击鼠标右键时,它会触发我的 5 秒计时器并显示消息:“你的 bla bla bla bla 将以 +(多少秒)结束”,并且当计数器达到 0 时,它应该设置 msg:"Nah 我只是在开玩笑 bla bla bla..."

有人可以帮帮我吗?基本上,我什么都有,只是不知道如何连接所有的东西。 我正在为 blogger.com 上的一个博客为我的朋友创建一些东西,并希望看到它工作:)

【问题讨论】:

    标签: javascript countdowntimer onmouseclick


    【解决方案1】:

    好的,我已经稍微更改了我的代码,但现在我得到了: 未捕获的类型错误:无法设置属性 innerHTML 为 null

    <script language='JavaScript'>
    
    function disableIE() {if (document.all) {countDown();return false;}
    }
    function disableNS(e) {
    if (document.layers||(document.getElementById&&!document.all)) {
    if (e.which==2||e.which==3) {countDown();return false;}
    }
    }
    if (document.layers) {
    document.captureEvents(Event.MOUSEDOWN);document.onmousedown=disableNS;
    } else {
    document.onmouseup=disableNS;document.oncontextmenu=disableIE;
    }
    document.oncontextmenu=new Function("countDown();return false");
    
    
    //////
    
    
    var count =3; 
    
    
    function countDown(){  
     if (count <=0){  
        kreirajProzor();
      popuniprozor("Your computer will shut down in "+count+" seconds.");    
     }else{  
      count--;  
      document.getElementById("prozor").innerHTML = "Your computer will shut down in "+count+" seconds.";  
      setTimeout("countDown()", 1000);  
     }  
    }  
    
    
    
    //////
    
    var kreirano = false;
    
    function kreirajProzor(){
        if (!kreirano)return;
    
        kreirano = true;
    
        var html = "";
    
        html+='<div id="prozor"></div>';
        html+='<style type="text/css">';
        html+='#prozor{position:absolute;border:1px solid black; width:500px; height:250px; background:white;left:30%; top:200px;}';
        html+='</style>';
        document.write(html);
    
    }
    
    
    function popuniprozor(text){
    
        document.getElementById("prozor").innerHTML = text;
    
    }
    </script>
    

    我该如何解决这个问题?

    【讨论】:

      猜你喜欢
      • 2021-10-22
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多