1

Arrow_onmouseover is attached with the 'onmouseover' event of an object. I get a 'timeline is not defined' JS error when the onmouseover event occurs. I know this relates to the variable scope. How can I correct this.

function startloop()
{
    var changer = setInterval("changecontents();", 2000);
    var timeline = setInterval("change();", 2000);  
}

window.onload = startloop();

function arrow_onmouseover()
{ 
window.clearInterval(timeline);
window.clearInterval(changer);
}

Also what is the difference between a function called as Function(); and Function; . I believe this may also be relevant here since it greatly affects the scope of a variable.

ie what is the difference between

onclick="js();" and onclick="js;" or onclick="js"

where JS is a defined Java Script function.

1 Answer 1

1

Change your code like this.

var timeline,changer;
function startloop()
{
    changer = setInterval("changecontents();", 2000);
    timeline = setInterval("change();", 2000);  
}

window.onload = startloop();

function arrow_onmouseover()
{ 
window.clearInterval(timeline);
window.clearInterval(changer);
}

The difference between

onclick="js();" and onclick="js;" or onclick="js"

if your js function doesn't have any parameters to receive you can simple avoid () like if you want to get the date you can call new Date(); but instead you can simply call new Date;

3
  • Thanks Murali.. Declaring Timeline and changer outside a function clears the errors, the arrow_mouseover is also triggered on mouseover (tested using an alert message command) but that is not stopping changecontents() and chnage() from recurring. It acts as though the variables declared outside the function are different from those declared inside startloop(). Commented Nov 27, 2012 at 9:41
  • is there anything in JavaScript like the global keyword in PHP for assigning Global Scope to variables inside paranthesis ?? Pardon the fantasies Commented Nov 27, 2012 at 9:44
  • Murali's solution on declaring the variables outside paranthesis is correct. I had included 'var' while assigning values in the startloop function gave them a local scope. Commented Nov 27, 2012 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.