/    Sign up×
Community /Pin to ProfileBookmark

function only plays well by itself

hi,
i have created a function to change the border or a div in a nice easing manner, i have got my code to do this, the only problem is if the function is called again by another div calling the same function, its all seems to go pear shaped.

see my code below save it to a new html file and it should work on I.E, FF and SAFAFI but the same issue is also present.

[code=php]
<html>
<head>
<script language=”javascript”>

function bIn(idName,bStart,bEnd){

var aCtual = parseInt(document.getElementById(idName).style.borderWidth);
function doIt(){
var aCtual = parseInt(document.getElementById(idName).style.borderWidth);

var diff=bEnd-aCtual;
var tChange=aCtual+Math.round(diff/2);

document.getElementById(idName).style.border= “red “+tChange+”px solid”;

if(tChange==bEnd){
clearInterval(time);
}
}

time = setInterval(doIt,25);

}

function bOut(idName,bStart,bEnd){

var aCtual = parseInt(document.getElementById(idName).style.borderWidth);
function doNot(){
var aCtual = parseInt(document.getElementById(idName).style.borderWidth);

var diff=aCtual-bStart;
var tChange=aCtual-Math.round(diff/2);

document.getElementById(idName).style.border= “red “+tChange+”px solid”;

if(tChange==bStart){
clearInterval(time);
}
}

if(aCtual==bEnd){
clearInterval(time);
time = setInterval(doNot,25);
}

}
</script>
<style>
body{
margin-top:2px;
}
div.container{
position:relative;
margin-left:auto;
margin-right:auto;
cursor:hand;
}
div.box{
width:50px;
height:50px;
background:#000666;
float:left;
color:#FFFFFF;
font-size:20px;
font-weight:bold;
font-family:arial;
text-align:center;
vertical-align:bottom;
}
div.motion{
clear:both;
width:50px;
height:50px;
background:#000666;
position:absolute;
color:#FFFFFF;
font-size:20px;
font-weight:bold;
font-family:arial;
cursor:hand;
}
</style>
</head>
<body>
<div class=”container”>
<div class=”box” style=”border:0px solid red;” id=”smelly” onMouseOver=”bIn(this.id,0,10)”

onMouseOut=”bOut(this.id,0,10)”>one</div>
<div class=”box” style=”border:0px solid red;” id=”smelly1″ onMouseOver=”bIn(this.id,0,10)”

onMouseOut=”bOut(this.id,0,10)”>two</div>
<div class=”box” style=”border:0px solid red;” id=”smelly2″ onMouseOver=”bIn(this.id,0,10)”

onMouseOut=”bOut(this.id,0,10)”>three</div>
<div class=”box” style=”border:0px solid red;” id=”smelly3″ onMouseOver=”bIn(this.id,0,10)”

onMouseOut=”bOut(this.id,0,10)”>four</div>
<div class=”box” style=”border:0px solid red;” id=”smelly4″ onMouseOver=”bIn(this.id,0,10)”

onMouseOut=”bOut(this.id,0,10)”>five</div>
</div>
</body>
</html>
[/code]

hope the answer is a simple one, if you want me to try and break down the code further i shall, but im asuming the code will be easy to follow for you gurus.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@AsperonJun 19.2008 — hope the answer is a simple one, if you want me to try and break down the code further i shall, but im asuming the code will be easy to follow for you gurus.[/QUOTE]

first of all we all aren't gurus

what do you mean by it all goes pear shaped...what happens, if you give a more specific problem it'll be easier to find the solution
Copy linkTweet thisAlerts:
@shakeukauthorJun 19.2008 — basically the functions stop working all together but i dont know why.

you can test it yourself to see the result, i.e just playing with 1 of the divs works then when you move your mouse over them all its stops working.
Copy linkTweet thisAlerts:
@shakeukauthorJun 20.2008 — is there anyone who could help?
Copy linkTweet thisAlerts:
@shakeukauthorJun 20.2008 — anyone? this is really bugging me.

thanks
Copy linkTweet thisAlerts:
@shakeukauthorJun 20.2008 — still no help? ?
Copy linkTweet thisAlerts:
@GSharpJun 20.2008 — Instead of time, do var time... helps a little.

I think the real issue is calling the same function with the same name multiple times stops the first one from finishing, which leaves the code in invalid states.
Copy linkTweet thisAlerts:
@shakeukauthorJun 20.2008 — is there anyway i can sort this then? its been wracking my brain.
Copy linkTweet thisAlerts:
@GSharpJun 21.2008 — Here's my theory: Javascript is multi-threaded in most newer browsers, so you are ending up in the same function at the same time, which is messing up the values of your variables and leaving you in invalid states. I can't think of any way around it the way you are doing it. You might just have to give each div it's own function. Or maybe try an already existing library that already does what you're trying to do? Like scriptaculous.
×

Success!

Help @shakeuk spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...