Forum Discussion
Using JavaScript with True/False Buttons
In short , the problem is that every time the learner clicks an item, the javascript runs and adds it to the list. The solution is that you can get a more accurate number by counting at the end, instead of trying to constantly adjust the variable up and down as things change, and you only have to do it once. Trying to adjust on the fly is difficult, fraught with the possibility for errors and duplicates, and very complicated if the learner changes their mind.
If it were mine, here's what I'd do:
1. Create a Calculate layer for each restaurant, and one for dessert and one for drinks.
2. Don't attach any triggers to the food items. Create a trigger for Dessert to set NextSlide to "dessert" when Dessert is clicked, and one to set NextSlide to :"drinks" when Beverages is clicked
3. On each slide (each restaurant, dessert, and drinks) have one trigger that shows the Calculate layer when the learner is ready to move on. (Clicks Dessert, Drinks, or checkout.)
4. Set those layers to have .5 sec timeline, Not hide other layers, Hide themself when the timeline ends.
5. On the Calculate layer, create triggers to set all the variables to 0 (or false) when the timeline starts.
6. Create triggers to set the variables to appropriate values when the timeline on the layer reaches .2 if the state of the button is selected. This will work even if a layer is showing and the buttons are on the base, as long as you haven't created any button sets. For the number variables, use the trigger to add, for example, FlourTortillaCalories to TotalCalories if the Flour Tortilla button is selected, etc. Once you have the numbers all added up, leave them alone. For the T/F variables, set, for example, FlourTortilla to True if the FlourTortilla button is selected.
7. Create a trigger to hide the Calculate layer when the timeline ends on the Calculate layer.
8.Create a trigger to jump to the Desserts if NextSlide = "dessert", and one to jump to Beverages if NextSlide = "drinks" when the timeline ends on the layer.
9. On the Results slide, instead of List_Smash, List_Chipotle, and List_Panera, I would have 1 variable : List_Food, and the same for Calories, Carbs, Fats, and Proteins
10. Consolidate all the javascripts into one script that is executed when the timeline starts on the Results slide. Get the player once, then set all the text variables, concatenating the appropriate text if the corresponding variable is True. The numeric variables are already set.
Once the learner is ready to move from a slide, the layer calculates which items are selected, and sets the corresponding variables. When they checkout, the javascript builds the lists, just like it is doing now. No worries about when what happens, or, more importantly, no possibility of duplicating items.
It appears that the javascript is probably working correctly, if the right information is passed to it, and it only runs once. Still, Math will probably find some ways to streamline it.
Any questions, ask.