List Info

Thread: Javascript Objects and setInterval()




Javascript Objects and setInterval()
user name
2006-05-04 02:29:15
Hey, I've been trying to get this to work, but can't seem
to. There
must be some inconsistency with the scope of the functions.
Could I get
some suggestions, or solutions?

Here is my code:

function kwObject ()
{
	var layer;
	var fader;

	// Fade the opacity to 1.0 over a given time;
	this.fadeIn = function (oTime)
	{
		if (fader) clearInterval(fader);
		fader = setInterval("fade(1, 1/oTime)", 10);
	}

	// Fade the opacity to 0.0 over a given time;
	this.fadeOut = function (oTime)
	{
		if (fader) clearInterval(fader);
		fader = setInterval('fade(-1, 1/oTime)', 10);
	}

	// Proform fading
	this.fade = function (dir, chunk)
	{
		if (layer && dir < 0.0 &&
layer.style.opacity > 0.0)
		{
			layer.style.opacity = layer.style.opacity - chunk;
			alert(dir+", "+layer.style.opacity);
		}
		else if (layer && dir > 0.0 &&
layer.style.opacity < 1.0)
		{
			layer.style.opacity = parseInt(layer.style.opacity) +
chunk;
			alert(dir+", "+layer.style.opacity);
		}
		else
		{
			clearInterval(fader);
		}
	}

	this.setLayer = function (layerId)
	{
		layer = document.getElementById(layerId);
		layer.style.opacity = 1.0;
	}
}

It's pretty straight forward to figure out what I am trying
to do. I
just can't figure out why the setInterval doesn't call the
function
thats inside the "Object". Here is how you set
up an instance of it:

<html>
<head>
	<title>Layer Fade Test</title>
	<script type="application/x-javascript"
src="Object.js" />
	<script type="application/x-javascript">
		var object;
		function setup () {
			object = new kwObject();
			object.setLayer("data");
		}
	</script>
</head>
<body onload="setup()">
	<div id='data'
onclick="object.fadeOut(100)">Fade
Me</div>
</body>
</html>

Thanks so much for your help.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "OOP Javascript" group.
To post to this group, send email to OOP-Javascriptgooglegroups.com
To unsubscribe from this group, send email to
OOP-Javascript-unsubscribegooglegroups.com
For more options, visit this group at http://
groups.google.com/group/OOP-Javascript
-~----------~----~----~----~------~----~------~--~---

Javascript Objects and setInterval()
user name
2006-05-04 02:53:23
Terribly sorry for my redundant topic, granted the actual
answer was a
bit harder to find. I suggest reading Richard Cornford's
first response
as it is extremely well stated:

http:/
/groups.google.com/group/comp.lang.javascript/browse_thread/
thread/727d3b1d8f72a733/ede81a5c3aa13499


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "OOP Javascript" group.
To post to this group, send email to OOP-Javascriptgooglegroups.com
To unsubscribe from this group, send email to
OOP-Javascript-unsubscribegooglegroups.com
For more options, visit this group at http://
groups.google.com/group/OOP-Javascript
-~----------~----~----~----~------~----~------~--~---

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )