Stop a Delay and increment ?! Set Timeout Help please.

Discussion in 'JavaScript' started by fdoze, May 9, 2010.

  1. #1
    Hi,

    I'm using mootools to create a delay set time out on a function everytime someone clicks a button.

    My question is how can I stop executing the function and reset a time out delay to start over.

    Imagine this: Click and start the time out 10 seconds to do something. But I if click again after 5 seconds I want to restart the counting to the next 10 seconds.

    The problem is that I can not reset the time out so I get the function executed 5 seconds after second click, 10 seconds after the first click, and 10 seconds again after the second click.

    And what I want is the function to be executed after the last click...

    Got it?

    Here are some experimental javascript mootools code that I was using to try to workarround this:
    	var isClicked = 0;
    	var doIsClicked = function() {
    		
    		isClicked++;
    		
    		(function(){
    		  
    				isClicked = 0;
    				
    		  		//alert("-1-"+isClicked)
    				
    		 }).delay(15000);
    		
    		//alert(isClicked);
    		
    	}
    	var doStandByShowDelay = function() {
    		
    		doIsClicked();
    		
    		(function(){
    		  
    		  		//alert("-2 -"+isClicked)
    				if ( isClicked == '0' ){
    						doStandByShow();
    		  			}
    					else{
    						doStandByShowDelay();
    					}
    		 }).delay(10000);
    		
    	}
    
    Code (markup):
    So, is there anyway to CANCEL a time out Delay so I can start over from zero again????

    Thanks!!
     
    fdoze, May 9, 2010 IP
  2. skier88

    skier88 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm not familiar with mootools syntax, so I apologize if this is not quite applicable. However, when dealing with this problem in plain javascript I found that you have to initialize a global variable, then set it to the timeout. To clear it you first have to use clearTimeout(var), then set the var to the new timeout. For example:

    
    var timeout = '';
    function startTimer()
    {
    clearTimeout(timeout);
    timeout = setTimeout('dostuff()',3000);
    }
    
    Code (markup):
     
    skier88, May 9, 2010 IP
  3. bprashanth.gi

    bprashanth.gi Active Member

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    93
    #3
    I would agree with skier88

    Having a global variable for timeout and updating it everytime some click event occurs would be a great idea...

    I suppose you can pass the variable as a parameter to the delay statement like

    delay(timeoutvariable);

    All the best!
     
    bprashanth.gi, May 9, 2010 IP
  4. fdoze

    fdoze Peon

    Messages:
    205
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi all!

    I will test clearTimeout(timeout);


    Many thanks!
     
    fdoze, May 10, 2010 IP
  5. fdoze

    fdoze Peon

    Messages:
    205
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    When I set
    
    myStandByTimeout = setTimeout(doStandByShow(),30000);
    
    Code (markup):
    Under mootools is executed without the delay.

    I can only have a delay when I use
    
    myStandByTimeout = (function(){
    									 
    				    doStandByShow();
    					
    					}).delay(3000);
    
    Code (markup):
    The thing is.. Hoe can I use clearSetTimeout this way!?
     
    fdoze, May 10, 2010 IP
  6. fdoze

    fdoze Peon

    Messages:
    205
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I gos this from Mootolls Docs:

    var myTimer = myFunction.delay(5000); //Waits 5 seconds then executes myFunction.
    myTimer = $clear(myTimer); //Cancels myFunction.

    I will test.

    Thanks,.
     
    fdoze, May 10, 2010 IP
  7. Patrick van Thiel

    Patrick van Thiel Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi,

    I'm a newby in javascript.

    I have found some javascript to move a dif

    i have some HTML :
    <p><a onmouseover="javascript:test()" onmouseout="javascript:out()" >Test div move 1</a> </p>
    <p><a onmouseover="javascript:test1()" onmouseout="javascript:out()" >Test div move 2</a> </p>
    <p><a onmouseover="javascript:test2()" onmouseout="javascript:out()" >Test div move 3</a> </p>
    <p><a onmouseover="javascript:test3()" onmouseout="javascript:out()" >Test div move 4</a> </p>
    
    Code (markup):
    and some javascript functions :
    function test(){
    $("box").move({delay:600,to:{left:-1200,top:0}});}
     
    function test1(){
    $("box").move({delay:600,to:{left:-800,top:0}});}
    
    function test2(){
    $("box").move({delay:600,to:{left:-400,top:0}});}
    
    function test3(){
    $("box").move({delay:600,to:{left:-0,top:0}});}
    
    function out1(){
    $("box").move({delay:100,to:{left:-1600,top:0}});}
    
    function out() {
    	clearTimeout('mydelay');
    	mydelay = setTimeout('out1()',2000);}
    
    Code (markup):
    the timeout has to be reset whene i hover over an other link, but the clearTimeout does nothing.
    can anyone please help me what's wrong.

    If nessesary i can place the complete code.
     
    Patrick van Thiel, Mar 14, 2011 IP