Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery Countdown timer with mousemove
    primarykey
    data
    text
    <p>I am trying to create a web page with a timer that is about 60 seconds long and if the user moves the mouse they get one message and if they wait the full 60 seconds they get another message. I have this part working which you can see from the code that has been created below.</p> <p>The part that I cant get working it to allow the user to try again so if they move the mouse then they get the chance to try letting the mouse stay still for another 60 seconds or even at the end where they get the win message they can try again also.</p> <p><strong>Old Code - The Code:</strong></p> <pre><code>&lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;Timer&lt;/title&gt; &lt;style&gt; .start{display:block;} .oops{display:none;} .end{display:none;} .tryagain{ cursor:pointer; color:#0066FF;} .startcount{ cursor:pointer; color:#0066FF;} &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div class="start"&gt; &lt;span class="startcount"&gt;Start text&lt;/span&gt; || Can you wait??? &lt;p class="countdown"&gt;&lt;/p&gt; &lt;/div&gt; &lt;div class="oops"&gt; opps you moved the mouse | Would you like to &lt;span class="startcount"&gt;try again?&lt;/span&gt; &lt;/div&gt; &lt;div class="end"&gt; Well done you waited. | Would you like to &lt;span class="startcount"&gt;try again?&lt;/span&gt; &lt;/div&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js"&gt;&lt;/script&gt; &lt;script&gt; $(function(){ var count = -1; countdown = null; $('.startcount').on('click', function(){ count = 10; $('.start').css({'display':'block'}); $('.end').css({'display':'none'}); $('.oops').css({'display':'none'}); if (countdown == null){ countdown = setInterval(function(){ $("p.countdown").html(count + " seconds remaining!"); if(count &gt; 0){ count--; } if(count == 0){ $('.start').css({'display':'none'}); $('.end').css({'display':'block'}); $('.oops').css({'display':'none'}); clearInterval(countdown); countdown = null; } if(count &gt; 1){ $('html').mousemove(function(){ $('.oops').css({'display':'block'}); $('.start').css({'display':'none'}); $('.end').css({'display':'none'}); clearInterval(countdown); countdown = null; }); }else if(count == 0){ $('html').mousemove(function(){ $('.oops').css({'display':'none'}); $('.end').css({'display':'block'}); }); }else{ $('html').mousemove(function(){ $('.oops').css({'display':'none'}); }); } console.log(count); console.log(countdown); }, 1000); } }); }); &lt;/script&gt; &lt;/body&gt; </code></pre> <p><strong>Fixed Code:</strong></p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;Timer&lt;/title&gt; &lt;style&gt; .start{display:block;} .oops{display:none;} .end{display:none;} .tryagain{ cursor:pointer; color:#0066FF;} .startcount{ cursor:pointer; color:#0066FF;} &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div class="start"&gt; &lt;span class="startcount"&gt;Start text&lt;/span&gt; || Can you wait??? &lt;p class="countdown"&gt;&lt;/p&gt; &lt;/div&gt; &lt;div class="oops"&gt; opps you moved the mouse | Would you like to &lt;span class="startcount"&gt;try again?&lt;/span&gt; &lt;/div&gt; &lt;div class="end"&gt; Well done you waited. | Would you like to &lt;span class="startcount"&gt;try again?&lt;/span&gt; &lt;/div&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js"&gt;&lt;/script&gt; &lt;script&gt; $(function(){ var count = 3, countdown = null, counter; $('.startcount').on('click', function(){ // on click displayes the start information $('.start').css({'display':'block'}); $('.end').css({'display':'none'}); $('.oops').css({'display':'none'}); counter = count; // sets up the counter countdown = setInterval(function(){ $("p.countdown").html(counter + " seconds remaining!"); if(counter != 0){ //checks for mouse movements $('html').mousemove(function(){ $('.oops').css({'display':'block'}); $('.start').css({'display':'none'}); $('.end').css({'display':'none'}); clearInterval(countdown); //clears the interval so that it does not run multiple times }); } else { $('html').unbind('mousemove'); //removes the mousemove bind to the html !this is important $('.start').css({'display':'none'}); $('.end').css({'display':'block'}); $('.oops').css({'display':'none'}); clearInterval(countdown); //clears the interval so that it does not run multiple times return false; } counter--; //removes 1 from the counter }, 1000); $('html').unbind('mousemove'); //removes the mousemove bind to the html !this is important }); }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload