tag:blogger.com,1999:blog-6483791709556439408.post6869330583172851488..comments2016-04-30T23:10:40.442-07:00Comments on $ sudo FIXIT!!: Random Question Generator on Anki using JavaScriptRunasSudohttp://www.blogger.com/profile/08533859490094306079noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-6483791709556439408.post-79740662819461861262016-04-04T10:00:59.752-07:002016-04-04T10:00:59.752-07:00I seem to be getting window.py consistent *between...I seem to be getting window.py consistent *between cards*. I'll see if I can't fix this by nulling o.data in the "back" side's if statement.Anonymoushttps://www.blogger.com/profile/13865500978849184413noreply@blogger.comtag:blogger.com,1999:blog-6483791709556439408.post-21248043804722113582016-01-07T18:14:43.745-08:002016-01-07T18:14:43.745-08:00Sorry, I'm not sure I understand your question...Sorry, I'm not sure I understand your question. That snippet of code allows "data persistence between the two execution of Javascript", so "each time the code is called, the same number is returned". It works for me.<br /><br />Regarding 'var o = (typeof py === "undefined") ? {} : py;', Anki Desktop exposes a Javascript object "window.py". It doesn't have any useful functions for us, but it is shared between front and back executions of Javascript on the same card, so can be used to share data between them. The code there checks if window.py is available, and if so, uses it, otherwise just provides a dummy object.RunasSudohttps://www.blogger.com/profile/08533859490094306079noreply@blogger.comtag:blogger.com,1999:blog-6483791709556439408.post-65173715831758713462015-10-18T07:48:46.634-07:002015-10-18T07:48:46.634-07:00Actually, very clever idea !
This is would be ver...Actually, very clever idea !<br /><br />This is would be very nice if we could have data persistence between the two execution of Javascript.<br /><br />Unfortunately, I'm not able to reproduce the effect of your last snippet of code:<br />window.setTimeout(function() {<br />var o = (typeof py === "undefined") ? {} : py; //Attempt to use Anki's Bridge object to store data across sides.<br />o.data = o.data || {};<br /><br />o.data.num = o.data.num || Math.random();<br />if (document.getElementById("front")) document.getElementById("front").innerHTML = "The number on the front is " + o.data.num;<br />if (document.getElementById("back")) document.getElementById("back").innerHTML = "The number on the back is " + o.data.num;<br />}, 0); //Execute after Anki has loaded its Bridge object.<br />}, 0); <br /><br />In my attempt, it seems that the value of Math.random() is retained during one execution of Anki (so, each time the code is called, the same number is returned)<br /><br />Could you also please explain the signification of var o = (typeof py === "undefined") ? {} : py; ? Why "undefined" ? What is "py" ?<br /><br />I'm looking forward to your answer, thanks !<br /><br />cidrolinhttps://www.blogger.com/profile/06738958712579253019noreply@blogger.com