CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript frameworks (http://www.codingforums.com/forumdisplay.php?f=62)
-   -   [jQuery] setTimeout issue (http://www.codingforums.com/showthread.php?t=174099)

schotte 08-09-2009 08:16 PM

[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

edric 08-10-2009 04:16 PM

Quote:

Originally Posted by schotte (Post 850947)
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.

Uzbekjon 08-13-2009 01:08 PM

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);
}



All times are GMT +1. The time now is 09:02 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.