PDA

View Full Version : returning an event is this the best way to do it?



panvulcon
May 21st, 2007, 06:31 PM
hi,

i'm having trouble returning an event. i've writen this code from scratch but i aint sure if its the best way to acheive what i want.

i would like to a have a function that runs every second and some variables to decrease every time the function is called (automatically) and if interupted by onKeyDown the variables will decrease at a different rate.

can anybody shed some light?

here my code

y = 1000
f = 500
dy = 10
d2y = 0

var al = document.getElementById("altitude")
al.innerHTML = (("Your altitude is ") + (y))
var fu = document.getElementById("fuel")
fu.innerHTML = (("You have ") + (f) + ("L of fuel left."))

function start(e) //refers to the event
{
if (y > 0 && f > 0)
{
var keynum
var time=new Date()
seconds=time.getSeconds()
d2y = d2y + seconds
y = y - 10

//if(window.event) // IE
//{
// keynum = e.keyCode
//}
if(e.which) // Netscape/Firefox/Opera
{
keynum = e.which
}

if (keynum == 65)//a
{
y = y + 4
f = f - 4
}

var al = document.getElementById("altitude")
al.innerHTML = (("Your altitude is ") + (y))
var fu = document.getElementById("fuel")
fu.innerHTML = (("You have ") + (f) + ("L of fuel left."))
var tim = setTimeout("start()",1000)
}////////need to return e

return this.(e)
}

thanks

andy

glenngv
May 21st, 2007, 06:37 PM
Why do you have a return statement outside of a function? Is the whole thing inside a function? If so, you just need to do this.

return e;

But you need to pass the e to the start() function in setTimeout.

tim = setTimeout(function(){start(e)}, 1000);