Results 1 to 5 of 5
  1. #1

    JavaScript break

    Hi, I want to break a function from ouside the actual function.


    Well, I have a setTimeOut function which makes a function run after x seconds. I then want that function to stop running after the user tells it to.

    For example, If a user hovers over a link, in two seconds a JavaScript alert will say 'Hello'. If the user hovers over another link, that will cause the function to terminate.

    I am not sure if you can break a function from outside of the function, but if it could be done, could you please tell me how to do it.

    Thank you,

  2. #2
    Join Date
    Jan 2004
    I have not tested it and I'm not a javascript guru, but this comes to mind...
    var flgStop;
    function StartTimer(){
         setTimeout("DoSomething", 2000);
         flgStop = false;
    function DoSomething(){
    The mouseover can call StartTimer while flgStop can be set to true to disable.

    Let me know if it works.

  3. #3
    Thanks for that ACW, although it is not quite what I wanted. I wanted to terminate the function before it had the chance for the setTimeOut to finish and call upon the next function.

    I have had an idea how t do it now, just a case of making it work, which I expect it wont.

    Thanks anyways, any other ideas would be most welcome.


  4. #4
    Join Date
    Oct 2002
    j = setTimeout("DoSomething", 2000);
    if (something) clearTimeout (j) ;

  5. #5
    I have got it working, I used intervals instead of timeouts though. However, it works fine apart from the second time you try and do it, then the clearInterval doesnt seem to work. As it is for a drop down menu, it is kinda annoying.

    Any help to how I could fix it would be greatly appreciated, or if you could PM me, I will show you the code and then you could help me further.


Posting Permissions

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