FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Javascript Variables FRIH$ to Debug

 


coreymanshack
<html>
<head>
<script language="javascript">
function doit(id, opacStart, opacEnd, millisec){
var id = id;
var opacStart = opacStart;
var opacEnd = opacEnd;
var millisec = millisec;
setTimeout("opacity();", 1000);
}
function opacity() {
//speed for each frame
var speed = Math.round(millisec / 100);
var timer = 0;

//determine the direction for the blending, if start and end are the same nothing happens
if(opacStart > opacEnd) {
for(i = opacStart; i >= opacEnd; i--) {
setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
timer++;
}
} else if(opacStart < opacEnd) {
for(i = opacStart; i <= opacEnd; i++)
{
setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
timer++;
}
}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
var object = document.getElementById(id).style;
object.opacity = (opacity / 100);
object.MozOpacity = (opacity / 100);
object.KhtmlOpacity = (opacity / 100);
object.filter = "alpha(opacity=" + opacity + ")";
}
</script>
</head>
<body>
<a href="#" onmouseover="javascript:doit('button1', 100, 0, 500)"
onmouseout="javascript:doit('button1', 0, 100, 500)">
<img src="button1.gif" id="button1" border="0">
</a>
</body>
</html>




anyone have a clue why my variables aren't getting passed and why this isn't working?


Last edited by coreymanshack on Sat Sep 23, 2006 6:19 am; edited 1 time in total
nikolic
Set an alert at both events and check the values at both events. The values are likely passed, but the events are likely changed when the object is changed by your DHTML.
coreymanshack
It pissed me off and i dont feel like debugging it again... i just want someon to fix it for me... i'll giv em some frihost...
Kaneda
If you declare a variable with the "var" keyword, it's declared local to the function scope (unless you declare it in the global scope Wink). So, these variables only exist inside the doit function.

Code:

function doit(id, opacStart, opacEnd, millisec){
  // THESE VARIABLES ARE ALL LOCAL TO doit() - can't be accessed from the opacity() function.
  var id = id;
  var opacStart = opacStart;
  var opacEnd = opacEnd;
  var millisec = millisec;
  setTimeout("opacity();", 1000);
}


Several ways to get around that. Use a closure, and set the timeout as:

Code:

function doit(id, opacStart, opacEnd, millisec){
  // THESE VARIABLES ARE ALL LOCAL TO doit() - but also to the anonymous function
  // (closure), defined inside doit() - hence, that function can access their values.
  var id = id;
  var opacStart = opacStart;
  var opacEnd = opacEnd;
  var millisec = millisec;
  setTimeout(function() {
    opacity(id,opacStart,opacEnd,millisec);
  },1000);
}


... then modify the opacity function to take those parameters.

Or, simpler (if you're not familiar with closures, they tend to seem counterintuitive in their grabbing of the parent function's scope), just declare id, opacStart, opacEnd and millisec in the global scope, outside the function:

Code:

var id;
var opacEnd;
var opacStart;
var millisec;

function doit(_id, _opacStart, _opacEnd, _millisec) {
  // Renamed the arguments to avoid name clashing)
  id = _id;
  opacStart = _opacStart;
  opacEnd = _opacEnd;
  millisec = _millisec;
  setTimeout(opacity,1000);
}
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.