0

I have created a drag and drop game. Is there a possible way to stop all the mouseEvents when the time is out?

Below are my codes for the timer.

var canvas = document.getElementById("canvas");
var canvas_context = canvas.getContext("2d");
var text = "you have: 10 : 00 left"

function showFillText() {
canvas_context.clearRect(500, 350, 80, 100);
canvas_context.fillStyle = '#36F'; //text color
canvas_context.font = ' bold 20px sans-serif';
canvas_context.textBaseline = 'bottom'; 
canvas_context.fillText(text, 500, 450); //('text', x, y)
}
var mins = .1;  //Set the number of minutes you need
var secs = mins * 60;
var currentSeconds = 0;
var currentMinutes = 0;
setTimeout('Decrement()',1000);

function Decrement() {
    currentMinutes = Math.floor(secs / 60);
    currentSeconds = secs % 60;
    if(currentSeconds <= 9) currentSeconds = "0" + currentSeconds;
    secs--;
   text = currentMinutes + ":" + currentSeconds; //Set the element id you need the time put into.
    if(secs !== -1) setTimeout('Decrement()',1000);
    //document.getElementById("timerText").innerHTML = currentMinutes + ":" + currentSeconds; 
    if (currentMinutes == 0 && currentSeconds ==0)  {
    text = "TIMES UP"   
    //document.location.href = "http://www.google.com";
    }
    showFillText()
}
3
  • Can you be more specific. You don't actually want to disable all mouse events, or your site will just stop working. Commented Nov 5, 2013 at 14:54
  • 1
    FYI it's much better to pass function references to setTimeout instead of strings: setTimeout(Decrement, 1000); Commented Nov 5, 2013 at 14:54
  • 1
    Please also show your event binding. Commented Nov 5, 2013 at 14:54

3 Answers 3

2

Keep a list of all your event handlers and delete them when your timer expires.

So instead of:

document.getElementById("some element").addEventListener("onX", function () {});

you do:

registerEvent(document.getElementById("some element"), "onX", function () {});

registerEvent keeps a list of all your event listeners. You can remove event listeners with removeEventListener.

Sign up to request clarification or add additional context in comments.

Comments

0

It is possible using pointer-events. Use it against a div or class you are interested in. http://caniuse.com/pointer-events

Comments

0

Add a flag called (eg) 'lock', initially set to 0.

When the time is up, set it to 1.

add an if within the function

function myFunction(){
    if( lock == 0 ){
        ... function ...
    }
}

There might be a more sophisticated way of doing it but that should work . . .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.