...

View Full Version : [jQuery] setTimeout issue



schotte
08-09-2009, 08:16 PM
Hey all,

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



$.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
Hey all,

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



$.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



$.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...


function heartbeat() { ... };

$.Plugin = function(obj,opt)
{
init();

function init() { ... };

setTimeout('heartbeat();', 1000);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum