LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-22-2007, 12:18 AM   #1
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
JavaScript: How to have a method call itself.


I have a class constructor in my script which creates a loop function. And in that loop function, I would like it to call itself through setTimeout(). It seems that this may not be possible, unless there is another way to do this, which is what my question is asking.

Is there a way to accomplish my goal?

example class:
Code:
function theGame ()
{
private:
    /* ... */

public:
    /* set methods   */
    /* get methods   */
    /* other methods */

    this.loop ()
    {
        /* do stuff */
        // tried all the following, one by one, not in the same block of code.
        globalVarName = setTimeout ( function () { this.loop (); }, 1 );
        globalVarName = setTimeout ( function () { loop ();      }, 1 );
        this.globalVarName = setTimeout ( function () { this.loop (); }, 1 );
        globalVarName = setTimeout ( function () { loop (); }, 1 );
    }

}
A while loop won't work, since it is an infinite loop and disables me from using my web browser.

I did fix my problem, by using an external function that handled the looping. But It looks out of place. And so I would like to know if there is a way to have setTimeout call a class method, inside the class the method is in.
 
Old 05-22-2007, 12:19 PM   #2
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 200

Rep: Reputation: 30
Use setInterval() function to call your function periodically. Recursive calls are not the greatest idea in JS.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript object static method? gsbarry Programming 2 11-11-2006 06:36 AM
Call to undefined method SimpleXMLElement::getName() firemankurt Programming 0 08-12-2006 01:36 AM
How do I call javascript from c++? kalleanka Programming 3 04-27-2006 08:07 AM
call on a javascript var ??? paul_mat Programming 1 02-15-2006 08:51 PM
How can I tell one object to do a method in javascript? eantoranz Programming 1 10-29-2004 01:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration