Your Universal Remote Control Center
RemoteCentral.com
Philips Pronto Professional Forum - View Post
Up level
Up level
The following page was printed from RemoteCentral.com:

Login:
Pass:
 
 

Original thread:
Post 2 made on Wednesday October 21, 2020 at 16:22
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,997
By default, when you leave a page or an activity, any script that is running on them will be cleared. The page script will be executed upon entry to a page and then again after the repeat interval (milliseconds). Therefore, I would strongly recommend defining variables that your page needs at the activity level.

Also, I would recommend you see the Page.onEntry and Page.onExit documentation in the Dev Guide.

Let's try a little experiment.

Create a new activity.

In the Activity script, define a variable named counter and a function to display and increment.


var counter=0;

function displayAndIncrement() {
var w = GUI.widget('MyCounterValue');
if (w) {
w.label = '' + counter;
}
counter +=1;
}


On a page in same Activity, create a label widget with the ProntoScript name of 'MyCounterValue'.

Then, in the Advanced Tab of same page, set script to non repeat. In the script area, have it call the function you defined earlier.


displayAndIncrement();


When you simulate the config, you should see a value of '0' displayed in your widget.

Next, on the same page, add a button. In the Buttons' Prontoscript, have it call the same function.


displayAndIncrement();


Now, every time you press the button, you should see the counter go up.


Finally, change your page script to repeat ever 1 second.

When you run/simulate the config, the counter value in the widget will increase by 1 every 1 second.

Now, add a 2nd page to the activity such that you can Page Up/Down between the pages.

Now page up/down between the pages. You should see that the counter value is maintained because it is defined at the Activity level and not at the Page level.

To illustrate why we put the function and counter variable in the Activity vs in the Page, simply copy the variable declaration and the function to your page that has the repeat interval set such that the script looks like this.


// BEGIN copied from Activity
var counter=0;

function displayAndIncrement() {
var w = GUI.widget('MyCounterValue');
if (w) {
w.label = '' + counter;
}
counter +=1;
}
// END copied from Activity

// Now, call the function.
displayAndIncrement();


What you should now see is that if you press that Button you created, the counter will incremement but as soon as the page script executes again, the value resets to 0.

This whole exercise illustrates the scope chain in Javascript and how variable/function lookups apply.

Global
PS Library
Activity
Page
Button

Finally, on that page you just moved stuff into (the one with the repeat interval), comment out the variable declaration line (NOTE - This will only work if you still have the variable declared at Activity Level meaning that you copied and did not remove the script in the Activity).


// BEGIN copied from Activity
//var counter=0;

function displayAndIncrement() {
var w = GUI.widget('MyCounterValue');
if (w) {
w.label = '' + counter;
}
counter +=1;
}
// END copied from Activity
displayAndIncrement();


Simulate and you should see the counter increment every 1 second or so (depending on the Page Repeat Interval).

Also note that I am using GUI.widget vs CF.widget to lookup the widget. This is because Activity script can execute before a page is loaded and ready to be displayed. At one point and time, I posted a thread about the differences between CF.widget and GUI.widget.

[Link: remotecentral.com]

You will want to see the last post of the first link in the above thread.

[Link: remotecentral.com]

Lyndel
Lyndel McGee
Philips Pronto Addict/Beta Tester


Hosting Services by ipHouse