[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

setTimeout in class function

Posted on 2006-06-03
7
Medium Priority
?
360 Views
Last Modified: 2008-02-01
Why is the timeout only executed once?

function test()
{
      var tickCounter = 0;
      
      this.tick = function()
      {                                                
                        
            tickCounter++;
            
            alert(tickCounter);
            
            setTimeout(this.tick, 1000);
      }      
}

in index.htm:

var t = new test();
t.tick()

navigator says "useless Timeout" and IEX says "invalid argument"
So what do I need to do to fix it?
0
Comment
Question by:Robse
7 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 16824018
Try using
setTimeout('tick', this, 1000);

-r-
0
 
LVL 10

Expert Comment

by:0h4crying0utloud
ID: 16824025


You setTimeout function take string (name of a funcion), not a function object.
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 1000 total points
ID: 16824060
The setTimeout statement is executed in global context and not in the Object context.
Therefore is this. not defined in global context (or it does reference to the function Object)
Perhps it helps to see the running version:
<script>

function test(){
     var tickCounter = 0;
     
     this.tick = function(gObjName)
     {                                        
                   
          tickCounter++;
         
          alert(tickCounter);
          setTimeout("self['"+gObjName+"'].tick('"+gObjName+"')", 1000);
     }    
}


var t = new test();
t.tick('t')

</script>

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:third
ID: 16824068
try to add parenthesis to the function,

setTimeout(this.tick(), 1000);
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16824080
Try in your browser before recommending infinite loops ;-)
0
 
LVL 30

Expert Comment

by:third
ID: 16824087
yep. now i'm using task manager to end the process... ;-) my bad.
0
 

Author Comment

by:Robse
ID: 16824303
lol third yeah I tried that too :)

Thanks Zvonko for your explanation and good solution!

Robse
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question