The Timer Class, For Object-oriented Timeouts

Nov 27, 2002

The problems with the setTimeout and setInterval functions provided in Javascript are twofold. First, you can't call a local object method without losing your scope, and second, you can't pass objects to the function, since the function call is implemented as a string.

The Timer class solves these difficulties by employing a static array to store the parent object and function arguments until the function is called.

This class is provided as-is and pro bono, so go ahead and muck with it if you see things that could be done better.

Thanks to WA for giving me the idea for this (albeit indirectly)!

Updated 4/18/2003: Footprint decreased, minor code improvements.
Updated 5/3/2003: Minor comment clarification; no code changes.
Updated 5/10/2003: Minor code improvements.
// The constructor should be called with
// the parent object (optional, defaults to window).

function Timer(){
this.obj = (arguments.length)?arguments[0]:window;
return this;
}

// The set functions should be called with:
// - The name of the object method (as a string) (required)
// - The millisecond delay (required)
// - Any number of extra arguments, which will all be
// passed to the method when it is evaluated.

Timer.prototype.setInterval = function(func, msec){
var i = Timer.getNew();
var t = Timer.buildCall(this.obj, i, arguments);
Timer.set[i].timer = window.setInterval(t,msec);
return i;
}
Timer.prototype.setTimeout = function(func, msec){
var i = Timer.getNew();
Timer.buildCall(this.obj, i, arguments);
Timer.set[i].timer = window.setTimeout("Timer.callOnce("+i+");",msec);
return i;
}

// The clear functions should be called with
// the return value from the equivalent set function.

Timer.prototype.clearInterval = function(i){
if(!Timer.set[i]) return;
window.clearInterval(Timer.set[i].timer);
Timer.set[i] = null;
}
Timer.prototype.clearTimeout = function(i){
if(!Timer.set[i]) return;
window.clearTimeout(Timer.set[i].timer);
Timer.set[i] = null;
}

// Private data

Timer.set = new Array();
Timer.buildCall = function(obj, i, args){
var t = "";
Timer.set[i] = new Array();
if(obj != window){
Timer.set[i].obj = obj;
t = "Timer.set["+i+"].obj.";
}
t += args[0]+"(";
if(args.length > 2){
Timer.set[i][0] = args[2];
t += "Timer.set["+i+"][0]";
for(var j=1; (j+2)<args.length; j++){
Timer.set[i][j] = args[j+2];
t += ", Timer.set["+i+"]["+j+"]";
}}
t += ");";
Timer.set[i].call = t;
return t;
}
Timer.callOnce = function(i){
if(!Timer.set[i]) return;
eval(Timer.set[i].call);
Timer.set[i] = null;
}
Timer.getNew = function(){
var i = 0;
while(Timer.set[i]) i++;
return i;
}
Here's an example of the code in action:
function Ticker(){
this.count = 0;
this.timer = new Timer(this);
}
Ticker.prototype.tick = function(d){
this.count+=d;
window.status = ""+this.count;
this.timer.setTimeout("tick", 1000, d);
}

window.onload = function(){
var ticker = new Ticker();
ticker.tick(1);
}

View 27 Replies


ADVERTISEMENT

New Object Oriented Design ?

Jul 30, 2011

I was writing my classes in an old fashion until today when I came across a new design pattern. Javascript is not an established OOP like C++ or Java. It doesn't have any easy way to create classes and to make private/public methods or properties as well as class-constructor. But it could be done in many ways.

My old way of OO design in Javascript was like following:

Code:

Those who were trying to write constructor and access public vars from private or vice versa, you would have probably faced some minor difficulties. But it was doable.

Now, the challenge is how can we make things cleaner and better. Here's the procedure I came up with:

Code:

View 14 Replies View Related

Object Oriented Javascript Programming

Feb 28, 2003

I just released javascript lib that really helps to develop robust and clear js-scenarios following the OOP directions. The library is compatible with wide range of browsers on different platforms, including Netscape 4.x, Netscape 6.x, Netscape 7.x, Opera 6, Opera 7, Mozilla 1.0, IE 4, IE 5, IE 6.

View 3 Replies View Related

Any Books For Object Oriented Javascript

Feb 15, 2006

Any websites or good books to learn real heavy object oriented javascript?

View 1 Replies View Related

Object Oriented - SetChecked Is Not A Function?

Apr 10, 2009

I'm pretty new to programming object oriented javascript and I'm wondering if I could get some quick advice.

Code JavaScript:

When I do checkAll(true), it checks all of the athletes, but, it throws an error saying:

Why is it saying setChecked is not a function?

View 1 Replies View Related

OO JS And SoundManager2 - Wrap Around Object Oriented Js

May 9, 2010

I will be the first to admit, I don't know much about JS and am just starting to wrap my mind around object oriented js. I have this small bit of code that has been giving me problems for days.

playlistItems[numItems-1] = soundManager.createSound({
id : songId,
url : $(this).attr('href'),
whileloading : player.whileloading,
onfinish: next
});

"next" is a function on the same object that playlistItems is defined on, but it doesn't ever seem to fire. I tried this.next as well, but it just gives me a runtime error....

View 1 Replies View Related

JavaScript Object-Oriented Programming

Nov 4, 2001

Can someone tell me all the OOP's that can be made from javascript.

View 3 Replies View Related

JQuery :: Object Oriented - This.callMe(); Is Not A Function

May 29, 2010

How to do this:

function MyObject(){

Error messages I am getting on above are: this.callMe(); is not a function callMe(); is not defined

View 4 Replies View Related

Events - Convert Some Procedural Code To Be More Object Oriented

Jul 9, 2009

I'm trying to convert some Procedural code to be more Object Oriented and I'm stuck...

The page is really simple:

It's just a big picture w/ a caption under it, and a few thumbnail pics off to the side, that when clicked, replace the big picture as well as swap out the caption for a new one.

In the code (external JS file):

I've got 2 arrays:

One that holds the thumbnail image file URLs, and the other that holds the captions.

And a function:

I've created an object prototype that I can instantiate in order to:

1.) create the event: do the actual swapping of the thumbnail, and the changing of the caption.

2.) and attach that event to the thumbnail's link onclick event handler.

The code below is the closest I've come to making it work. The problem is that when I click any of the thumbnail links it only executes the last event instead of the one that corresponds to the link that was just clicked. Hopefully someone can take a look at my code and let me know what I'm doing wrong.

Simplified version of the code:

The x,y,z variable assignments are only in the window.onload function for the sake of clarity, but the event object instantiation block has to be in there for anything to work at all.

I know that the event0,event1,event2 objects work, and are attached to the onclick handler because they will swap the last thumbnail and caption when I click any of the thumbnail links. Of course that's the problem now... any click only activates the last event. It's like it attached the last event to all the onclick event handlers.

In fact, if I only create the event0 object, it works like it's supposed to... but when I create the event1 object, it does what it's supposed to and it takes over for event0. Creating event2 makes it take over for all of them. Each subsequent event# object seems to attach its own addy[ ] and caption[ ] to each previous x[ ] link.

View 7 Replies View Related

Object Oriented Programming - Make A Round Corners Script

Feb 15, 2012

I am trying to make a round corners script in object oriented programming method. This is purely for learning purposes.

The script is no where near complete but I am already having problems with it.

I am trying out the techniques described in 'David Flanagan ' text book 'JavaScript: The Definitive Guide, 5th Edition'

This code is called from html page which once working will place a round container around element

The selector parameter in Custom_rounded_container function is defined because only the else part of the if statement in Custom_rounded_container is executing. I put this in because few people at other forums thought the problem was because of the selector parameter being undefined.

External Javascript file

View 1 Replies View Related

Round Corners Script In Object Oriented Programming Method

Feb 12, 2012

I am trying to make a round corners script in object oriented programming method. This is purely for learning purposes.

The script is no where near complete but I am already having problems with it.

I am trying out the techniques described in 'David Flanagan ' text book 'JavaScript: The Definitive Guide, 5th Edition'

This code is called from html page which once working will place a round container around element

Code:

Javascript functions below

Code:

View 19 Replies View Related

Object Oriented JS - OO Programming - Create Search Objects Or Arrays

Feb 10, 2011

Trying to figure out OO programming for JavaScript and I'm totally lost.

Here's what I'm trying to do:

I'm posting search data to a PHP form to run a query on a database.

The structure is sort of like this:

So I'm thinking I need to create search objects or arrays. Initially, I thought I could do something like this:

Code:

This doesn't seem to work. It says object not defined whenever I try to do something.

I've also tried creating a JSON object like this:

Code:

This will allow me retrieve the data like I want to:

Code:

The problem is I can't figure out how to add data to the JSON object without manually typing it in. I need to be able to loop through form elements on my HTML page and set them to these variables. I'm using jQuery and doing something like this:

Code:

View 7 Replies View Related

Object Oriented - Use "var Self" Technique

Dec 22, 2009

I am doing a script, like:

I have anonymous function "xhr.onreadystatechange = function() {}" in the method "this.update" of an object that receives 2 arrays through AJAX. I need these 2 arrays to be assigned to this.images and this.folders, respectively, through this anonymous function. I try to use "var self" technique, but it doesn't work.

View 11 Replies View Related

Canceling All Timeouts In One?

Oct 7, 2009

Is there a simple way to cancel all active timeouts without the need to reference each one individually ?

View 1 Replies View Related

Adding Timeouts To This Function?

Jun 8, 2010

Basically what I want to achieve is a timeout on "over" followed by a subsequent timeout when the timeout is being removed....

I have got as far as:

Code:

function toggleMenu(el, over)
{
if (over) {
setTimeout(Element.addClassName(el, 'over'), 200);

[code]...

View 12 Replies View Related

'nesting' Timeouts Or Delaying Until Functions End

Nov 29, 2010

I'm running into issues while trying to make some simple code to loop through and display different combinations of 3 colours.I want to show 3 boxes on screen and then loop through/increment each possible combination of colours.The problem with timeouts is although I can create a delay until the cycle on one level has finished before executing code to change the layer above it, this isn't enough - each level of loop has to increment just a single step after the layer "below" it completes a cycle. So I think I need some hybrid between nested for loops and timeouts (if such a beast exists).

View 7 Replies View Related

JQuery :: Monkey-patch The Ajax Calls To Deal With A Microsoft IE - Timeouts ?

Dec 4, 2011

This is really more a IE issue than jquery but I am using jquery.

Has anyone used this setTimeouts method from the IXMLHttpRequest2 object from IE.

I want to monkey path jquery calls with this call below. I tried this and I still get a property method error even though the method seems to be available. Has anyone experienced this?

With this Javascript code, I get this error "Object does not support this property or method" error. I checked that the methods are available and I do a check that the method is there. Am I sending the wrong number of args? Has anyone used this?

View 1 Replies View Related

JQuery :: Getting CSS Class As Object?

Feb 14, 2011

Is there anyway to have a CSS class returned as an object? This would be rather useful to feed, for example, to the .animate() function. There is no way to animate directly to a CSS class as destination, is there?

View 5 Replies View Related

Changing The Class Of An Object?

May 21, 2011

i have this code for changing the class of object 1 as mouse comes over object 2, the class changes back when mouses goes out.

Code:
<SCRIPT type="text/javascript">
function changeClass(floor3)
{
var obj = document.getElementById(floor3);

[Code].....

View 3 Replies View Related

Members Of The Error Class/object

Apr 18, 2006

What're the members of the error class/object? Is there a complete reference to all JavaScript objects available on the Web?

View 6 Replies View Related

How Do I Access The Object Instance From Within Class

Apr 22, 2007

I have a javascript object which dynamically generates a table adding, deleting and moving rows as the user clicks on buttons or links. Problem is when I generate a table row and add the javascript method call to my class, I have to put the object instance name variable of the class in order for it to be called from the onclick=function(). This is seriously limiting, but I'm stuck for a way round it. Heres a edited of the code so you get the idea....

Instantiating the object :-

var dtl = new DynamicTableList("table1", $
{myObject.allFieldsAsJavaScriptArray}, true, true, true);


My javascript class DynamicTableList, note the dtl javascript object
instance variable being referred to in the addRow function. How can I
avoid this???

function DynamicTableList(thisObjName, tableName, options, showDelete,
showUp, showDown)
{
this.processRow = function(r, row, up, down)
{
...
};

this.processRows = function()
{
...
};

this.getVisibility = function(visible)
{
...
};

this.delRow = function(button)
{
...
};

this.addRow = function(selection)
{
...

if (showDelete)
{
var cell2 = document.createElement('TD');
var inp2 = document.createElement('IMG');
/
************************************************** ***********************************************/
inp2.onclick=function(){dtl.delRow(this);} // Have to specify
dtc!!!!!!!!
/
************************************************** ***********************************************/
inp2.title='Delete'
inp2.alt='Delete'
inp2.src='images/delete.gif'
cell2.appendChild(inp2);
row.appendChild(cell2);
}

...

tbody.appendChild(row);

this.processRows();
};

this.moveRow = function(node, vector)
{
};
}

Obviously the code dtl.delRow(this); is being dynamically generated, but how do I replace the dtl instance name with something that'll work whatever the user of this class calls the instance of it!

View 2 Replies View Related

JQuery :: Get ID Of Every Object In Class On Each Loop?

Feb 26, 2010

I have an each loop that goes through all the elements in a certain class. What I need to do is get the ID of the current element in the each loop, but I can't seem to figure out how to. var currentID =$(this).attr ("id"); was my first assumption, but it's not working. [code]The css is just for debugging, not the goal, so alternate ways for me to do that won't help.

View 1 Replies View Related

JQuery :: Toggle Css Class Of Object

Oct 25, 2011

I am working on a Master-Detail grid that allows users to click on master rows to show or hide details rows. [code]What I want to do is to add Expandable class to SummaryRow if it's collapsed, add Collapsible class if it's expanded. But I don't know how to do that.

View 1 Replies View Related

New Object From User-defined Class In IE8?

Nov 18, 2010

I've been writing a slideshow script that I've had working perfectly on my mac in Chrome and Safari, however I went to look at it in IE8 and lo and behold it falls over. It says 'object doesn't support this property or method' in relation to the new SlideShow line in the main() function.

View 3 Replies View Related

Class Array In Custom Object Not Working?

Jan 19, 2010

I have created a class array (or variable that contains an array) and am trying to add an element to the array when a new Element object is created. Thus, the line of code to add a new element to the array is within the Element object's constructor function. However, the browser just gives me an error "object expected on line 9", the line of code trying to add an element to the array.

Are arrays not functional within objects? Or, is it that the array is made as a class variable?

Code:
function Element() {
// INSTANCE VARIABLES
this.name = "newElement" + Element.total_elements_created; // GIVE UNIQUE NAME
// INITIALIZATIONS
Element.element_count ++; // INCREASE TOTAL NUMBER OF ELEMENTS EXISTING

[Code]...

View 4 Replies View Related

Call Member Of Class Without Object Initiation?

Jun 22, 2011

Is there any way to call a method of a 'class' in without creating an instance of this class?

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved