Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    window.setTimeout kitty cat

    I think this is the first time I've ever posted a script out of morbid curiosity. Usually, the entire galaxy is at stake (and, of course, I blame you guys if I don't get it debugged ). This time, I just thought the question was weird enough to be worthy.

    Question: how do you test if a setTimeout has been cleared. For instance:

    var fred;

    function Joe () {
    fred = window.setTimeout("Joe();",2000);
    }

    function bob () {
    // whatever, but I can see fred!
    }


    somewhere in the html:
    <input type=button onClick="bob();">

    on click, can bob use fred to tell if joe is about to fire (that is, within 2000 milliseconds)?
    If at first you don't succeed, spend more time online researching javascript!
    Beck

  • #2
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    more, gimme more!

    In the spirit of making this even more interesting (and bringing this particular posting back to the top of the list of threads in the forum for a few more hours), I have an additional thought to the above...

    is there a way to redirect a window.setTimeout in the middle of the wait. For instance...

    var timer = window.setTimeout("bob();",5000);

    somehow set the timer to point to joe() instead, but in the same five seconds. Can it be done..?

    I await thy answer....
    If at first you don't succeed, spend more time online researching javascript!
    Beck

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Atlanta, GA.
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That timeout is never "cleared" because the same function is called recursively. If you add a third function you can do
    Code:
    var fred = -1;
    
    function joe()
    {
    	fred = setTimeout("fred=0;billy();",2000);
    }
    
    function billy()
    {
    	alert("timer fired");
    }
    
    function bob()
    {
    	if(fred == -1)
    		alert("Timer was never started");
    	else if(fred == 0)
    		alert("Timer completed");
    	else
    		alert("Timer running - but I don't know how long is left");
    }
    However, you could probably create a new Date() that is set when the timer starts and compare it to a new Date() when bob is called to check the time remaining.
    ------------------------------------------------------------
    Not sure what "in the same five seconds" means but I am assuming that if you start the timer - then click a button 2 seconds later you want joe() to execute 3 seconds after that (ie 5 seconds from when the timer started.
    Code:
    bob=function()
    {
    	alert("Bob");
    }
    
    joe=function()
    {
    	alert("joe");
    }
    
    doIt = bob;
    
    var timerId = null;
    doBob = function()
    {
    	doIt = bob;
    
    	if(timerId != null)
    		timerId = setTimeout("doIt();",5000);
    }
    doJoe = function()
    {
    	doIt = joe;
    
    	if(timerId  != null)
    		timerId = setTimeout("doIt();",5000);
    }
    Here, if you call doBob() and then 3 seconds later call doJoe() then the script will alert "joe" 2 seconds later which is 5 seconds after doBob() was initially called.

    This is done by using the var "doIt" which ends up being a pointer to the function to be executed.
    e.g.
    Code:
    doIt = bob;
    doIt();
    will alert "bob"
    Code:
    doIt = joe;
    doIt();
    will alert "joe"
    The answer does not come from thinking outside the box, it comes from realizing the truth :-
    "There Is No Box". [JavaScript Gadgets'n'Gizmos][JavaScript-FX]

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Why U cant C fred

    I think that the reason why you cant see fred is because It is within another function. Variables within a function are not set outside of that function. You would need to use a return statement.

  • #5
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    hmmm...

    fred is global, he can be seen from anywhere. No need return statements, he belongs to all, although I think if you create another fred variable inside some function, that particular instance of fred belongs to that function.

    RoyW I like your code style. Thanks for the ideas. Not sure when I might use them, but I didn't think of that first part, where you assign values to fred other than setTimeout return values.

    Koo'
    If at first you don't succeed, spend more time online researching javascript!
    Beck

  • #6
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sorry

    I missed the fred before the functions. So you are right beck.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •