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
    Regular Coder
    Join Date
    Aug 2004
    Posts
    280
    Thanks
    0
    Thanked 0 Times in 0 Posts

    set/clear timeout

    im using this code to clear or set timeout:
    (if it is not set, function do_it is called)
    but function does not work, any help?

    is very unrecommanded to use multiple .js files with functions?

    function play_stop () {
    kri = !(ref);
    if (kri) {
    do_it();
    change('play_d', 'stil/kv_plav.gif');
    }
    else {
    window.clearTimeout(ref);
    kricacref = false;
    change('play_d', 'stil/kv_zelen.gif');
    }
    }

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What's wrong with just using: if (!ref) {
    And I do not see where you are setting the setTimeout and/or if ref is global or local to the function it is defined in....

    .....Willy

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    When you do clearTimeout(ref), the ref variable will not be reset to 0, null or something. It retains its value (which is a number) and the setTimeout delay is cleared. So doing if (!ref) will not evaluate to true on succeeding execution. The solution is to reset it 0 or null manually after clearTimeout.
    Code:
    function play_stop () {
      if (!ref) {
        do_it();
        change('play_d', 'stil/kv_plav.gif');
      }
      else {
        window.clearTimeout(ref);
        ref = 0;
        kricacref = false;
        change('play_d', 'stil/kv_zelen.gif');
      }
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #4
    Regular Coder
    Join Date
    Aug 2004
    Posts
    280
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks guys, i used another function - setInterval

    em - one thing, if i want to have a global variable, i must set it outside the function, right?


    var myglobalvar;

    (some blah-blah);

    function whichusesusemyglobalvar () {
    };

  • #5
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i must set it outside the function

    yes.


    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #6
    Regular Coder
    Join Date
    Aug 2004
    Posts
    280
    Thanks
    0
    Thanked 0 Times in 0 Posts
    no, james, it is not so.

    you can set it this way(at least it works with IE and firefox 1.0):

    window.myglobalvariable = 'myblah';


  •  

    Posting Permissions

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