Skip to main content
Known Participant
November 24, 2016
Answered

HTML5 Canvas - Accessing variables across main timeline

  • November 24, 2016
  • 3 replies
  • 4902 views

If I set a function or variable on the first frame of the main timeline I don't appear to be able to access them on later frames.

So for example if on frame 1 I set the following variable:

var rootRef = this;

And the following function

function test()

{

     alert ("test");

}

If I try to refer to the variable rootRef on say frame 50, it is undefined

And if I try to call the test function I get the following error:
Uncaught ReferenceError: test is not defined(…)

I would ideally like to include as much code as possible on frame 1 so any advice how to make variables and functions available across the timeline?

This topic has been closed for replies.
Correct answer Colin Holgate

I believe that:

this.varname = value;

would be seen across the current timeline, and:

window.varname = value;

would be seen everywhere.

3 replies

P StevenAuthor
Known Participant
November 25, 2016

Thank you Colin and Gory.

How do I access a clip level variable within a function?

this.myVar = "Paul";

alert ("this.myVar = " + this.myVar);

function whatIsMyVariable()

{

  alert ("myVar = " + myVar);

}

whatIsMyVariable();

Trying the above gives me an error saying myVar is undefined.

If I make it a global as follows it works but I prefer to minimise the use of global variables

myVar = "Paul";

alert ("myVar = " + myVar);

function whatIsMyVariable()

{

  alert ("myVar = " + myVar);

}

whatIsMyVariable();

P StevenAuthor
Known Participant
November 25, 2016

I have sussed it

this.myVar = "Paul";

alert ("this.myVar = " + this.myVar);

this.whatIsMyVariable = function()

{

  alert ("function this.myVar = " + this.myVar);

}

this.whatIsMyVariable();

Inspiring
November 24, 2016
Colin Holgate
Inspiring
November 24, 2016

Although the last example of myVar = value should work globally, I've seen that lead to errors. By explicitly saying window.myVar = value the errors don't happen.

Inspiring
November 24, 2016

Thanks Colin. I've had no problems with it so far but good to know to how solve it if I come across it.

Colin Holgate
Colin HolgateCorrect answer
Inspiring
November 24, 2016

I believe that:

this.varname = value;

would be seen across the current timeline, and:

window.varname = value;

would be seen everywhere.