HELP! with JS wrong code

nidzela shared this question 10 years ago
Answered

dear comunity of GeoGebrads & JavaScripters...


i'm in great trouble (but i hope it will be small for so many of you).

next week there will be a wonderful meeting in Barcelona, and i've been asked to collaborate a little bit with a JS applet. BUT... my dear applet doesn't work as expected. it used to, but now it doesn't: i tried to improve it according to the event, but after adding some buttons and functions i spoilt it.


the file is a quite large simulation of dice draw, taken from a game called kiriki.

the first unwanted behaviours came because of the " " and the ' ', which i don't completely understand. then it got worse after trying to stop animation automatically (a feature that has been implemented in version 4, by the way). next fatal idea was to introduce a button to show/hide points... when i press randomly the stop and the hide buttons, the effect is stepping into those typical and annoying and unpredictable and unreasonable computer behaviours, such as not stoppping at the end point or losing all functionality of the buttons. not satisfied with all of this stuff, i even tried to introduce a textarea, which happens to work or dis-work at his own will (i asked for this once, and obtained a fine solution... temporally, it seems, as even this tiny success has gone down!).


i've decided to erase the "hide all" button, finally. but anyway...

would somebody be so kind to take a tour inside my html and illuminate my poor knowledge of what i've been trying to create? i really would appreciate that.


the best version i've been able to produce can be seen HERE. the script part is fully commented, and is all concentrated inbetween lines 40 up to 130, aprox.


thanks in advance!!

Comments (6)

photo
1

hello again, GeoGebrads & JavaScripters!


after a quite short-long time, my product has improved in a number of aspects.

but still, i can't make animation stop when the variable "e" reaches its limit (at 2000, to be precise).

fortunately this question is short enough to be asked for within the margins of this brief message:


    function ggbOnInit() {

    var applet = document.ggbApplet;

    applet.setAnimating("e", true);

    applet.setValue("Vel", 0.0);

    applet.startAnimation();

    applet.registerObjectUpdateListener("e", "ReiniE");

    }


    function ReiniE (Value) {

    var applet = document.ggbApplet;

    if(Value == 2000)

    {estat = applet.getValue("tr");

    Fals("tr");

    applet.setValue("Vel", 0.0);

    applet.setValue("tr", estat);

    }

    }


summarizing the code:

  • "e" is animated, but with variable velocity "Vel", which is set to 0 before switching on the animation.
  • afterwards, "e" needs to be listened to in order to know if it has reached 2000. in this case, and only, "Vel" must be set to 0 again.
  • but there is an action in the middle: a checkbox must be turned to false (and then reset to its previous value after the animation is stopped).

so... what's wrong with it?

photo
1

Hi

What is "tr"? a box case?a slider?

In ggb4 you can use javascript in the applet, it is easier. if you tell me what is "tr" in your applet , I can help you and finish this one.We can use boolean tostop animation at a value.see the texte above when e==100 here.


https://ggbm.at/547973


Daniel

https://ggbm.at/547973

photo
1

hi, monsieur Mentrard!


yes, "tr" is a checkbox that controls traces.

there are 15 points with "trace on" and their 15 respective copies with "trace off". the use of "tr" is to toggle visibility of both groups of points.


the code about "tr" was introduced to avoid unwanted traces in certain rare situations: for example, when animation was running with trace on, and global number of tosses was suddenly changed (see it in this previous version).


anyway, in my opinion the non-stopping animation bug doesn't come from this checkbox. i suspect that the real cause has something to do with the timer "e". when automatic animation is on, maybe "e" does not cover all of its range of values (there are many different actions to be performed at each step), or maybe it doesn't reach the precise value 2000, or maybe 2000 is reached for a too short time and the function ReiniE can't be properly evaluated...


or more probably, my code is wrong! in fact, it used to work "sometimes" in one of the first full versions (just try to turn animation on, without pressing any other button... and then try to move things, such as trace, regenerate, clear, adding points...). to be precise, i have doubts about how the function registerObjectUpdateListener("e", "ReiniE") works... is the use of Value correct, inside ReiniE?

    function ReiniE (Value) {

    var applet = document.ggbApplet;

    if(Value== 2000)

despite recognizing this is not a great bug, i want to understand behaviours of interaction between JS and applets, and particularly what makes my stopping process useless. and still i want to do it in GeoGebra 3.2, i'll have time to try version 4.0 once it is stable.


thanks a lot!

photo
1

i've just had a kind of 'enlightment' about registerObjectUpdateListener...


as the KnowHow section describes, the two parameters of registerObject are strings:

Whenever the object with the given name is updated, the JavaScript function JSFunctionName is called using the name of the updated object as its single argument

in fact, the syntaxis is precisely:

void registerObjectUpdateListener(String objName, String JSFunctionName)

yes, this was my doubt in plain words!

seems that my Value inside the function has to be used as a string. well: i've remade if(Value == 2000) into something like if(Valor + " == 2000")...


but it doesn't work, neither: animation does not even start. i need something like evalCommand(Valor + " == 2000") returning not the evaluation success (within GeoGebra), but the result of it!

so the doubt remains: what can i do next?

photo
1

Look at example 5 on the page you've linked, in particular:


    finished = ggbApplet.getValueString("finished");

photo
1

thanks for the advice, murkle!


your indication brought new hope to me, but i'm afraid i haven't been able to apply it to my situation despite the efforts. by the way, it is a very clever code, i had not noticed of that brilliant example before. there is a lot to see in it!


finally, i have chosen to go backwards in time and i've asked myself what i was trying to do in the begining: 'listening to "e" in order to control its speed'. i realised that, as Daniel suggested, it is easier to control speed with a couple of checkboxes, one for the user and the other for an auto-stop criterion. so i abandoned the idea of controlling speed from the JS, and focused on introducing two new checkboxes STOP1 and STOP2. anyway i kept the previous code within /* ... */, just in case it could be useful sometime.


the result is that the auto-stop feature works at about 10% of the times, kind of randomly. i think i won't be able to get much more for a quite long while... this is the renewed version, i consider it almost closed, pendant only of this elusive matter.


EDIT: arranged (same link as before for final version).

the solution came from a friend of mine, who saw the applet and suggested i should control first the same tecnique within a small construction. it was not working either, so it definitely drove me to a speed problem: somehow ggb and JS interaction can't assume too many calculations at the same time, and my condition could not be correctly processed. so i have defined a function for 'e' animation speed getting slower as it reaches the end, and the problem has been fixed.

speed: If[STOP1 ∨ STOP2, 0, If[e < 1950, 1.4, (2007.5 - e) / 50]]


thanks everybody!

© 2021 International GeoGebra Institute