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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2004
    Location
    Edinburgh, SCO
    Posts
    113
    Thanks
    0
    Thanked 0 Times in 0 Posts

    [jQuery] setTimeout issue

    Hey all,

    as I mentioned in the title, I've an issue getting setTimeout to work inside a plugin I am developing.

    Code:
    $.Plugin = function(obj,opt)
    {
         init();
         
         function init() { ... };
         function heartbeat() { ... };
    
         setTimeout('heartbeat();', 1000);
    }
    Error: heartbeat is not defined

    Anyone dealt with this before?

    Cheers
    Last edited by schotte; 08-10-2009 at 09:49 AM.

  • #2
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by schotte View Post
    Hey all,

    as I mentioned in the title, I've an issue getting setTimeout to work inside a plugin I am developing.

    Code:
    $.Plugin = function(obj,opt)
    {
         init();
         
         function init() { ... };
         function heartbeat() { ... };
    
         setTimeout('heartbeat();', 1000);
    }
    Error: heartbeat is not defined

    Anyone dealt with this before?

    Cheers
    try this

    Code:
    $.Plugin = function(obj,opt)
    {
         init();
         
         function init() { ... };
         function heartbeat() { ... };
    
         setTimeout(heartbeat, 1000);
    }
    no braces no parentheses just pass the function object itself instead of a string.

  • #3
    New Coder
    Join Date
    Feb 2009
    Location
    Uzbekistan
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The problem is that javascript can not find heartbeat() when it's being run. You should either declare your function out of the plugin (global namespace) or refer to your function with the full path ($.plugin.func()). Of course you'd need to rewrite your code a little...

    Code:
    function heartbeat() { ... };
    
    $.Plugin = function(obj,opt)
    {
         init();
         
         function init() { ... };
    
         setTimeout('heartbeat();', 1000);
    }


  •  

    Posting Permissions

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