cdocutils.nodes document q)q}q(U nametypesq}q(Xusing-callbacksqKXcallbacks to object methodsqNXusing callbacksqNXcreating a callback explicitlyq NXspecifying a callbackq NuUsubstitution_defsq }q Uparse_messagesq ]qUcurrent_sourceqNU decorationqNUautofootnote_startqKUnameidsq}q(hUusing-callbacksqhUcallbacks-to-object-methodsqhUid1qh Ucreating-a-callback-explicitlyqh Uspecifying-a-callbackquUchildrenq]q(cdocutils.nodes target q)q}q(U rawsourceqX.. _using-callbacks:qUparentq hUsourceq!cdocutils.nodes reprunicode q"XS/Data/Code/JavaScript/MathJax/Code/SourceForge/trunk/mathjax/docs/rst/callbacks.rstq#q$}q%bUtagnameq&Utargetq'U attributesq(}q)(Uidsq*]q+Ubackrefsq,]q-Udupnamesq.]q/Uclassesq0]q1Unamesq2]q3Urefidq4huUlineq5KUdocumentq6hh]q7ubcdocutils.nodes section q8)q9}q:(hUh hh!h$Uexpect_referenced_by_nameq;}q(h.]q?h0]q@h,]qAh*]qB(hheh2]qC(hheuh5Kh6hUexpect_referenced_by_idqD}qEhhsh]qF(cdocutils.nodes title qG)qH}qI(hXUsing CallbacksqJh h9h!h$h&UtitleqKh(}qL(h.]qMh0]qNh,]qOh*]qPh2]qQuh5Kh6hh]qRcdocutils.nodes Text qSXUsing CallbacksqTqU}qV(hhJh hHubaubcdocutils.nodes paragraph qW)qX}qY(hX4A "callback" is a function that MathJax calls when it completes an action that may occur asynchronously (like loading a file). Many of MathJax's functions operate asynchronously, and MathJax uses callbacks to allow you to synchronize your code with the action of those functions. The `MathJax.Callback` structure manages these callbacks. Callbacks can include not only a function to call, but also data to be passed to the function, and an object to act as the JavaScript `this` value in the resulting call (i.e., the object on which the callback is to execute).qZh h9h!h$h&U paragraphq[h(}q\(h.]q]h0]q^h,]q_h*]q`h2]qauh5Kh6hh]qb(hSXA "callback" is a function that MathJax calls when it completes an action that may occur asynchronously (like loading a file). Many of MathJax's functions operate asynchronously, and MathJax uses callbacks to allow you to synchronize your code with the action of those functions. The qcqd}qe(hXA "callback" is a function that MathJax calls when it completes an action that may occur asynchronously (like loading a file). Many of MathJax's functions operate asynchronously, and MathJax uses callbacks to allow you to synchronize your code with the action of those functions. The qfh hXubcdocutils.nodes title_reference qg)qh}qi(hX`MathJax.Callback`qjh(}qk(h.]qlh0]qmh,]qnh*]qoh2]qpuh hXh]qqhSXMathJax.Callbackqrqs}qt(hUh hhubah&Utitle_referencequubhSX structure manages these callbacks. Callbacks can include not only a function to call, but also data to be passed to the function, and an object to act as the JavaScript qvqw}qx(hX structure manages these callbacks. Callbacks can include not only a function to call, but also data to be passed to the function, and an object to act as the JavaScript qyh hXubhg)qz}q{(hX`this`q|h(}q}(h.]q~h0]qh,]qh*]qh2]quh hXh]qhSXthisqq}q(hUh hzubah&huubhSXT value in the resulting call (i.e., the object on which the callback is to execute).qq}q(hXT value in the resulting call (i.e., the object on which the callback is to execute).qh hXubeubhW)q}q(hXCCallbacks can be collected into :ref:`Queues ` where the callbacks will be processed in order, with later callbacks waiting until previous ones have completed before they are called. They are also used with :ref:`Signals ` as the means of receiving information about the signals as they occur.qh h9h!h$h&h[h(}q(h.]qh0]qh,]qh*]qh2]quh5Kh6hh]q(hSX Callbacks can be collected into qq}q(hX Callbacks can be collected into qh hubcsphinx.addnodes pending_xref q)q}q(hX:ref:`Queues `qh hh&U pending_xrefqh(}q(UreftypeqXrefqU reftargetqX using-queuesqU refdomainqXstdqh*]qh,]qU refexplicitqh.]qh0]qh2]qUrefdocqU callbacksquh5Kh]qcdocutils.nodes emphasis q)q}q(hhh(}q(h.]qh0]q(UxrefqhXstd-refqeh,]qh*]qh2]quh hh]qhSXQueuesqq}q(hUh hubah&UemphasisqubaubhSX where the callbacks will be processed in order, with later callbacks waiting until previous ones have completed before they are called. They are also used with qq}q(hX where the callbacks will be processed in order, with later callbacks waiting until previous ones have completed before they are called. They are also used with qh hubh)q}q(hX:ref:`Signals `qh hh&hh(}q(UreftypeqXrefqhX using-signalsqU refdomainqXstdqh*]qh,]qU refexplicitq͈h.]qh0]qh2]qhhuh5Kh]qh)q}q(hhh(}q(h.]qh0]q(hhXstd-refqeh,]qh*]qh2]quh hh]qhSXSignalsq܅q}q(hUh hubah&hubaubhSXG as the means of receiving information about the signals as they occur.q߅q}q(hXG as the means of receiving information about the signals as they occur.qh hubeubhW)q}q(hXA number of methods in `MathJax.Hub` and `MathJax.Ajax` accept callback specifications as arguments and return callback structures. These routines always will return a callback even when none was specified in the arguments, and in that case, the callback is a "do nothing" callback. The reason for this is so that the resulting callback can be used can be used in a `MathJax.Callback.Queue` for synchronization purposes, so that the actions following it in the queue will not be performed until after the callback has been fired.qh h9h!h$h&h[h(}q(h.]qh0]qh,]qh*]qh2]quh5Kh6hh]q(hSXA number of methods in q텁q}q(hXA number of methods in qh hubhg)q}q(hX `MathJax.Hub`qh(}q(h.]qh0]qh,]qh*]qh2]quh hh]qhSX MathJax.Hubqq}q(hUh hubah&huubhSX and qq}r(hX and rh hubhg)r}r(hX`MathJax.Ajax`rh(}r(h.]rh0]rh,]rh*]r h2]r uh hh]r hSX MathJax.Ajaxr r }r(hUh jubah&huubhSX8 accept callback specifications as arguments and return callback structures. These routines always will return a callback even when none was specified in the arguments, and in that case, the callback is a "do nothing" callback. The reason for this is so that the resulting callback can be used can be used in a rr}r(hX8 accept callback specifications as arguments and return callback structures. These routines always will return a callback even when none was specified in the arguments, and in that case, the callback is a "do nothing" callback. The reason for this is so that the resulting callback can be used can be used in a rh hubhg)r}r(hX`MathJax.Callback.Queue`rh(}r(h.]rh0]rh,]rh*]rh2]ruh hh]rhSXMathJax.Callback.Queuerr}r(hUh jubah&huubhSX for synchronization purposes, so that the actions following it in the queue will not be performed until after the callback has been fired.r r!}r"(hX for synchronization purposes, so that the actions following it in the queue will not be performed until after the callback has been fired.r#h hubeubhW)r$}r%(hX_For example, the :meth:`MathJax.Ajax.Require()` method can be used to load external files, and it returns a callback that is called when the file has been loaded and executed. If you want to load several files and wait for them all to be loaded before performing some action, you can create a `Queue` into which you push the results of the :meth:`MathJax.Ajax.Require()` calls, and then push a callback for the action. The final action will not be performed until all the file-load callbacks (which preceed it int he queue) have been called; i.e., the action will not occur until all the files are loaded.r&h h9h!h$h&h[h(}r'(h.]r(h0]r)h,]r*h*]r+h2]r,uh5K h6hh]r-(hSXFor example, the r.r/}r0(hXFor example, the r1h j$ubh)r2}r3(hX:meth:`MathJax.Ajax.Require()`r4h j$h&hh(}r5(Ureftyper6Xmethr7hXMathJax.Ajax.Requirer8U refdomainr9Xpyr:h*]r;h,]r<U refexplicitr=h.]r>h0]r?h2]r@hhUpy:classrANU py:modulerBNuh5K h]rCcdocutils.nodes literal rD)rE}rF(hj4h(}rG(h.]rHh0]rI(hj:Xpy-methrJeh,]rKh*]rLh2]rMuh j2h]rNhSXMathJax.Ajax.Require()rOrP}rQ(hUh jEubah&UliteralrRubaubhSX method can be used to load external files, and it returns a callback that is called when the file has been loaded and executed. If you want to load several files and wait for them all to be loaded before performing some action, you can create a rSrT}rU(hX method can be used to load external files, and it returns a callback that is called when the file has been loaded and executed. If you want to load several files and wait for them all to be loaded before performing some action, you can create a rVh j$ubhg)rW}rX(hX`Queue`rYh(}rZ(h.]r[h0]r\h,]r]h*]r^h2]r_uh j$h]r`hSXQueuerarb}rc(hUh jWubah&huubhSX( into which you push the results of the rdre}rf(hX( into which you push the results of the rgh j$ubh)rh}ri(hX:meth:`MathJax.Ajax.Require()`rjh j$h&hh(}rk(UreftyperlXmethrmhXMathJax.Ajax.RequirernU refdomainroXpyrph*]rqh,]rrU refexplicitrsh.]rth0]ruh2]rvhhjANjBNuh5K h]rwjD)rx}ry(hjjh(}rz(h.]r{h0]r|(hjpXpy-methr}eh,]r~h*]rh2]ruh jhh]rhSXMathJax.Ajax.Require()rr}r(hUh jxubah&jRubaubhSX calls, and then push a callback for the action. The final action will not be performed until all the file-load callbacks (which preceed it int he queue) have been called; i.e., the action will not occur until all the files are loaded.rr}r(hX calls, and then push a callback for the action. The final action will not be performed until all the file-load callbacks (which preceed it int he queue) have been called; i.e., the action will not occur until all the files are loaded.rh j$ubeubh8)r}r(hUh h9h!h$h&h=h(}r(h.]rh0]rh,]rh*]rhah2]rh auh5K,h6hh]r(hG)r}r(hXSpecifying a Callbackrh jh!h$h&hKh(}r(h.]rh0]rh,]rh*]rh2]ruh5K,h6hh]rhSXSpecifying a Callbackrr}r(hjh jubaubhW)r}r(hX`Callbacks can be specified in a number of different ways, depending on the functionality that is required of the callback. The easiest case is to simply provide a function to be called, but it is also possible to include data to pass to the function when it is called, and to specify the object that will be used as `this` when the function is called.rh jh!h$h&h[h(}r(h.]rh0]rh,]rh*]rh2]ruh5K.h6hh]r(hSX=Callbacks can be specified in a number of different ways, depending on the functionality that is required of the callback. The easiest case is to simply provide a function to be called, but it is also possible to include data to pass to the function when it is called, and to specify the object that will be used as rr}r(hX=Callbacks can be specified in a number of different ways, depending on the functionality that is required of the callback. The easiest case is to simply provide a function to be called, but it is also possible to include data to pass to the function when it is called, and to specify the object that will be used as rh jubhg)r}r(hX`this`rh(}r(h.]rh0]rh,]rh*]rh2]ruh jh]rhSXthisrr}r(hUh jubah&huubhSX when the function is called.rr}r(hX when the function is called.rh jubeubhW)r}r(hXFor example, the :meth:`MathJax.Ajax.Require()` method can accept a callback as its second argument (it will be called when the file given as the first argument is loaded and executed). So you can callrh jh!h$h&h[h(}r(h.]rh0]rh,]rh*]rh2]ruh5K5h6hh]r(hSXFor example, the rr}r(hXFor example, the rh jubh)r}r(hX:meth:`MathJax.Ajax.Require()`rh jh&hh(}r(UreftyperXmethrhXMathJax.Ajax.RequirerU refdomainrXpyrh*]rh,]rU refexplicitrh.]rh0]rh2]rhhjANjBNuh5K5h]rjD)r}r(hjh(}r(h.]rh0]r(hjXpy-methreh,]rh*]rh2]ruh jh]rhSXMathJax.Ajax.Require()rr}r(hUh jubah&jRubaubhSX method can accept a callback as its second argument (it will be called when the file given as the first argument is loaded and executed). So you can callrr}r(hX method can accept a callback as its second argument (it will be called when the file given as the first argument is loaded and executed). So you can callrh jubeubcdocutils.nodes literal_block r)r}r(hXqMathJax.Ajax.Require("[MathJax]/config/myConfig.js",function () { alert("My configuration file is loaded"); });rh jh!h$h&U literal_blockrh(}r(UlinenosrUlanguagerX javascriptrU xml:spacerUpreserverh*]rh,]rh.]rh0]rh2]ruh5K>h6hh]rhSXqMathJax.Ajax.Require("[MathJax]/config/myConfig.js",function () { alert("My configuration file is loaded"); });rr}r(hUh jubaubhW)r}r(hXand an alert will appear when the file is loaded. An example of passing arguments to the callback function includes the following:rh jh!h$h&h[h(}r(h.]rh0]rh,]rh*]rh2]r uh5K?h6hh]r hSXand an alert will appear when the file is loaded. An example of passing arguments to the callback function includes the following:r r }r (hjh jubaubj)r}r(hXyfunction loadHook (x) {alert("loadHook: "+x)} MathJax.Ajax.Require("[MathJax]/config/myConfig.js",[loadHook,"myConfig"]);rh jh!h$h&jh(}r(jjX javascriptrjjh*]rh,]rh.]rh0]rh2]ruh5KFh6hh]rhSXyfunction loadHook (x) {alert("loadHook: "+x)} MathJax.Ajax.Require("[MathJax]/config/myConfig.js",[loadHook,"myConfig"]);rr}r(hUh jubaubhW)r}r(hXvHere, the ``loadHook()`` function accepts one argument and generates an alert that includes the value passed to it. The callback in the :meth:`MathJax.Ajax.Require()` call is ``[loadHook,"myConfig"]``, which means that (the equivalent of) ``loadHook("myConfig")`` will be performed when the file is loaded. The result should be an alert with the text `loadHook: myConfig`.rh jh!h$h&h[h(}r(h.]r h0]r!h,]r"h*]r#h2]r$uh5KGh6hh]r%(hSX Here, the r&r'}r((hX Here, the r)h jubjD)r*}r+(hX``loadHook()``r,h(}r-(h.]r.h0]r/h,]r0h*]r1h2]r2uh jh]r3hSX loadHook()r4r5}r6(hUh j*ubah&jRubhSXq function accepts one argument and generates an alert that includes the value passed to it. The callback in the r7r8}r9(hXq function accepts one argument and generates an alert that includes the value passed to it. The callback in the r:h jubh)r;}r<(hX:meth:`MathJax.Ajax.Require()`r=h jh&hh(}r>(Ureftyper?Xmethr@hXMathJax.Ajax.RequirerAU refdomainrBXpyrCh*]rDh,]rEU refexplicitrFh.]rGh0]rHh2]rIhhjANjBNuh5KGh]rJjD)rK}rL(hj=h(}rM(h.]rNh0]rO(hjCXpy-methrPeh,]rQh*]rRh2]rSuh j;h]rThSXMathJax.Ajax.Require()rUrV}rW(hUh jKubah&jRubaubhSX call is rXrY}rZ(hX call is r[h jubjD)r\}r](hX``[loadHook,"myConfig"]``r^h(}r_(h.]r`h0]rah,]rbh*]rch2]rduh jh]rehSX[loadHook,"myConfig"]rfrg}rh(hUh j\ubah&jRubhSX', which means that (the equivalent of) rirj}rk(hX', which means that (the equivalent of) rlh jubjD)rm}rn(hX``loadHook("myConfig")``roh(}rp(h.]rqh0]rrh,]rsh*]rth2]ruuh jh]rvhSXloadHook("myConfig")rwrx}ry(hUh jmubah&jRubhSXY will be performed when the file is loaded. The result should be an alert with the text rzr{}r|(hXY will be performed when the file is loaded. The result should be an alert with the text r}h jubhg)r~}r(hX`loadHook: myConfig`rh(}r(h.]rh0]rh,]rh*]rh2]ruh jh]rhSXloadHook: myConfigrr}r(hUh j~ubah&huubhSX.r}r(hX.h jubeubhW)r}r(hXZThe callback for the :meth:`MathJax.Ajax.Require()` method actually gets called with a status value, in addition to any parameters already included in the callback specification, that indicates whether the file loaded successfully, or failed for some reason (perhaps the file couldn't be found, or it failed to compile and run). So you could userh jh!h$h&h[h(}r(h.]rh0]rh,]rh*]rh2]ruh5KNh6hh]r(hSXThe callback for the rr}r(hXThe callback for the rh jubh)r}r(hX:meth:`MathJax.Ajax.Require()`rh jh&hh(}r(UreftyperXmethrhXMathJax.Ajax.RequirerU refdomainrXpyrh*]rh,]rU refexplicitrh.]rh0]rh2]rhhjANjBNuh5KNh]rjD)r}r(hjh(}r(h.]rh0]r(hjXpy-methreh,]rh*]rh2]ruh jh]rhSXMathJax.Ajax.Require()rr}r(hUh jubah&jRubaubhSX' method actually gets called with a status value, in addition to any parameters already included in the callback specification, that indicates whether the file loaded successfully, or failed for some reason (perhaps the file couldn't be found, or it failed to compile and run). So you could userr}r(hX' method actually gets called with a status value, in addition to any parameters already included in the callback specification, that indicates whether the file loaded successfully, or failed for some reason (perhaps the file couldn't be found, or it failed to compile and run). So you could userh jubeubj)r}r(hXMathJax.Ajax.Require("[MathJax]/config/myConfig.js",function (status) { if (status === MathJax.Ajax.STATUS.OK) { alert("My configuration file is loaded"); } else { alert("My configuration file failed to load!"); } });rh jh!h$h&jh(}r(jjX javascriptrjjh*]rh,]rh.]rh0]rh2]ruh5K]h6hh]rhSXMathJax.Ajax.Require("[MathJax]/config/myConfig.js",function (status) { if (status === MathJax.Ajax.STATUS.OK) { alert("My configuration file is loaded"); } else { alert("My configuration file failed to load!"); } });rr}r(hUh jubaubhW)r}r(hXWto check if the file loaded properly. With additional parameters, the example might berh jh!h$h&h[h(}r(h.]rh0]rh,]rh*]rh2]ruh5K^h6hh]rhSXWto check if the file loaded properly. With additional parameters, the example might berr}r(hjh jubaubj)r}r(hXfunction loadHook (x,status) {alert("loadHook: "+x+" has status "+status)} MathJax.Ajax.Require("[MathJax]/config/myConfig.js",[loadHook,"myConfig"]);rh jh!h$h&jh(}r(jjX javascriptrjjh*]rh,]rh.]rh0]rh2]ruh5Keh6hh]rhSXfunction loadHook (x,status) {alert("loadHook: "+x+" has status "+status)} MathJax.Ajax.Require("[MathJax]/config/myConfig.js",[loadHook,"myConfig"]);rr}r(hUh jubaubhW)r}r(hXNote that the parameters given in the callback specification are used first, and then additional parameters from the call to the callback come afterward.rh jh!h$h&h[h(}r(h.]rh0]rh,]rh*]rh2]ruh5Kfh6hh]rhSXNote that the parameters given in the callback specification are used first, and then additional parameters from the call to the callback come afterward.rr}r(hjh jubaubh8)r}r(hUh jh!h$h&h=h(}r(h.]rh0]rh,]rh*]rhah2]rhauh5Klh6hh]r(hG)r}r(hXCallbacks to Object Methodsrh jh!h$h&hKh(}r(h.]rh0]rh,]rh*]rh2]ruh5Klh6hh]rhSXCallbacks to Object Methodsrr}r(hjh jubaubhW)r}r (hX When you use a method of a JavaScript object, a special variable called `this` is defined that refers to the object whose method is being called. It allows you to access other methods or properties of the object without knowing explicitly where the object is stored.r h jh!h$h&h[h(}r (h.]r h0]r h,]rh*]rh2]ruh5Knh6hh]r(hSXHWhen you use a method of a JavaScript object, a special variable called rr}r(hXHWhen you use a method of a JavaScript object, a special variable called rh jubhg)r}r(hX`this`rh(}r(h.]rh0]rh,]rh*]rh2]ruh jh]rhSXthisr r!}r"(hUh jubah&huubhSX is defined that refers to the object whose method is being called. It allows you to access other methods or properties of the object without knowing explicitly where the object is stored.r#r$}r%(hX is defined that refers to the object whose method is being called. It allows you to access other methods or properties of the object without knowing explicitly where the object is stored.r&h jubeubhW)r'}r((hX For example,r)h jh!h$h&h[h(}r*(h.]r+h0]r,h,]r-h*]r.h2]r/uh5Ksh6hh]r0hSX For example,r1r2}r3(hj)h j'ubaubj)r4}r5(hX~var aPerson = { firstname: "John", lastname: "Smith", showName: function () {alert(this.firstname+" "+this.lastname)} };r6h jh!h$h&jh(}r7(jjX javascriptr8jjh*]r9h,]r:h.]r;h0]r<h2]r=uh5K|h6hh]r>hSX~var aPerson = { firstname: "John", lastname: "Smith", showName: function () {alert(this.firstname+" "+this.lastname)} };r?r@}rA(hUh j4ubaubhW)rB}rC(hXIcreates an object that contains three items, a `firstname`, and `lastname`, and a method that shows the person's full name in an alert. So ``aPerson.fullName()`` would cause an alert with the text ``John Smith`` to appear. Note, however that this only works if the method is called as ``aPerson.showName()``; if instead you didrDh jh!h$h&h[h(}rE(h.]rFh0]rGh,]rHh*]rIh2]rJuh5K}h6hh]rK(hSX/creates an object that contains three items, a rLrM}rN(hX/creates an object that contains three items, a rOh jBubhg)rP}rQ(hX `firstname`rRh(}rS(h.]rTh0]rUh,]rVh*]rWh2]rXuh jBh]rYhSX firstnamerZr[}r\(hUh jPubah&huubhSX, and r]r^}r_(hX, and r`h jBubhg)ra}rb(hX `lastname`rch(}rd(h.]reh0]rfh,]rgh*]rhh2]riuh jBh]rjhSXlastnamerkrl}rm(hUh jaubah&huubhSXB, and a method that shows the person's full name in an alert. So rnro}rp(hXB, and a method that shows the person's full name in an alert. So rqh jBubjD)rr}rs(hX``aPerson.fullName()``rth(}ru(h.]rvh0]rwh,]rxh*]ryh2]rzuh jBh]r{hSXaPerson.fullName()r|r}}r~(hUh jrubah&jRubhSX$ would cause an alert with the text rr}r(hX$ would cause an alert with the text rh jBubjD)r}r(hX``John Smith``rh(}r(h.]rh0]rh,]rh*]rh2]ruh jBh]rhSX John Smithrr}r(hUh jubah&jRubhSXK to appear. Note, however that this only works if the method is called as rr}r(hXK to appear. Note, however that this only works if the method is called as rh jBubjD)r}r(hX``aPerson.showName()``rh(}r(h.]rh0]rh,]rh*]rh2]ruh jBh]rhSXaPerson.showName()rr}r(hUh jubah&jRubhSX; if instead you didrr}r(hX; if instead you didrh jBubeubj)r}r(hXtvar f = aPerson.showName; // assign f the function from aPerson f(); // and call the functionrh jh!h$h&jh(}r(jjX javascriptrjjh*]rh,]rh.]rh0]rh2]ruh5Kh6hh]rhSXtvar f = aPerson.showName; // assign f the function from aPerson f(); // and call the functionrr}r(hUh jubaubhW)r}r(hX#the association of the function with the data in ``aPerson`` is lost, and the alert will probably show ``undefined undefined``. (In this case, ``f`` will be called with ``this`` set to the ``window`` variable, and so ``this.firstname`` and ``this.lastname`` will refer to undefined values.)rh jh!h$h&h[h(}r(h.]rh0]rh,]rh*]rh2]ruh5Kh6hh]r(hSX1the association of the function with the data in rr}r(hX1the association of the function with the data in rh jubjD)r}r(hX ``aPerson``rh(}r(h.]rh0]rh,]rh*]rh2]ruh jh]rhSXaPersonrr}r(hUh jubah&jRubhSX+ is lost, and the alert will probably show rr}r(hX+ is lost, and the alert will probably show rh jubjD)r}r(hX``undefined undefined``rh(}r(h.]rh0]rh,]rh*]rh2]ruh jh]rhSXundefined undefinedrr}r(hUh jubah&jRubhSX. (In this case, rr}r(hX. (In this case, rh jubjD)r}r(hX``f``rh(}r(h.]rh0]rh,]rh*]rh2]ruh jh]rhSXfr}r(hUh jubah&jRubhSX will be called with rr}r(hX will be called with rh jubjD)r}r(hX``this``rh(}r(h.]rh0]rh,]rh*]rh2]ruh jh]rhSXthisrr}r(hUh jubah&jRubhSX set to the rr}r(hX set to the rh jubjD)r}r(hX ``window``rh(}r(h.]rh0]r h,]r h*]r h2]r uh jh]r hSXwindowrr}r(hUh jubah&jRubhSX variable, and so rr}r(hX variable, and so rh jubjD)r}r(hX``this.firstname``rh(}r(h.]rh0]rh,]rh*]rh2]ruh jh]rhSXthis.firstnamerr }r!(hUh jubah&jRubhSX and r"r#}r$(hX and r%h jubjD)r&}r'(hX``this.lastname``r(h(}r)(h.]r*h0]r+h,]r,h*]r-h2]r.uh jh]r/hSX this.lastnamer0r1}r2(hUh j&ubah&jRubhSX! will refer to undefined values.)r3r4}r5(hX! will refer to undefined values.)r6h jubeubhW)r7}r8(hXBecause of this, it is difficult to use an object's method as a callback if you refer to it as a function directly. For example,r9h jh!h$h&h[h(}r:(h.]r;h0]r<h,]r=h*]r>h2]r?uh5Kh6hh]r@hSXBecause of this, it is difficult to use an object's method as a callback if you refer to it as a function directly. For example,rArB}rC(hj9h j7ubaubj)rD}rE(hXvar aFile = { name: "[MathJax]/config/myConfig.js", onload: function (status) { alert(this.name+" is loaded with status "+status); } }; MathJax.Ajax.Require(aFile.name,aFile.onload);rFh jh!h$h&jh(}rG(jjX javascriptrHjjh*]rIh,]rJh.]rKh0]rLh2]rMuh5Kh6hh]rNhSXvar aFile = { name: "[MathJax]/config/myConfig.js", onload: function (status) { alert(this.name+" is loaded with status "+status); } }; MathJax.Ajax.Require(aFile.name,aFile.onload);rOrP}rQ(hUh jDubaubhW)rR}rS(hXwould produce an alert indicating that "undefined" was loaded with a particular status. That is because ``aFile.onload`` is a reference to the `onload` method, which is just a function, and the association with the `aFile` object is lost. One could dorTh jh!h$h&h[h(}rU(h.]rVh0]rWh,]rXh*]rYh2]rZuh5Kh6hh]r[(hSXiwould produce an alert indicating that "undefined" was loaded with a particular status. That is because r\r]}r^(hXiwould produce an alert indicating that "undefined" was loaded with a particular status. That is because r_h jRubjD)r`}ra(hX``aFile.onload``rbh(}rc(h.]rdh0]reh,]rfh*]rgh2]rhuh jRh]rihSX aFile.onloadrjrk}rl(hUh j`ubah&jRubhSX is a reference to the rmrn}ro(hX is a reference to the rph jRubhg)rq}rr(hX`onload`rsh(}rt(h.]ruh0]rvh,]rwh*]rxh2]ryuh jRh]rzhSXonloadr{r|}r}(hUh jqubah&huubhSX@ method, which is just a function, and the association with the r~r}r(hX@ method, which is just a function, and the association with the rh jRubhg)r}r(hX`aFile`rh(}r(h.]rh0]rh,]rh*]rh2]ruh jRh]rhSXaFilerr}r(hUh jubah&huubhSX object is lost. One could dorr}r(hX object is lost. One could dorh jRubeubj)r}r(hXJMathJax.Ajax.Require(aFile.name,function (status) {aFile.onload(status)});rh jh!h$h&jh(}r(jjX javascriptrjjh*]rh,]rh.]rh0]rh2]ruh5Kh6hh]rhSXJMathJax.Ajax.Require(aFile.name,function (status) {aFile.onload(status)});rr}r(hUh jubaubhW)r}r(hXbut that seems needlessly verbose, and it produces a closure when one is not really needed. Instead, MathJax provides an alternative specification for a callback that allows you to specify both the method and the object it comes from:rh jh!h$h&h[h(}r(h.]rh0]rh,]rh*]rh2]ruh5Kh6hh]rhSXbut that seems needlessly verbose, and it produces a closure when one is not really needed. Instead, MathJax provides an alternative specification for a callback that allows you to specify both the method and the object it comes from:rr}r(hjh jubaubj)r}r(hX2MathJax.Ajax.Require(aFile.name,["onload",aFile]);rh jh!h$h&jh(}r(jjX javascriptrjjh*]rh,]rh.]rh0]rh2]ruh5Kh6hh]rhSX2MathJax.Ajax.Require(aFile.name,["onload",aFile]);rr}r(hUh jubaubhW)r}r(hXThis requests that the callback should call ``aFile.onload`` as the function, which will maintain the connection between ``aFile`` and its method, thus preserving the correct value for `this` within the method.rh jh!h$h&h[h(}r(h.]rh0]rh,]rh*]rh2]ruh5Kh6hh]r(hSX,This requests that the callback should call rr}r(hX,This requests that the callback should call rh jubjD)r}r(hX``aFile.onload``rh(}r(h.]rh0]rh,]rh*]rh2]ruh jh]rhSX aFile.onloadrr}r(hUh jubah&jRubhSX= as the function, which will maintain the connection between rr}r(hX= as the function, which will maintain the connection between rh jubjD)r}r(hX ``aFile``rh(}r(h.]rh0]rh,]rh*]rh2]ruh jh]rhSXaFilerr}r(hUh jubah&jRubhSX7 and its method, thus preserving the correct value for rr}r(hX7 and its method, thus preserving the correct value for rh jubhg)r}r(hX`this`rh(}r(h.]rh0]rh,]rh*]rh2]ruh jh]rhSXthisrr}r(hUh jubah&huubhSX within the method.rr}r(hX within the method.rh jubeubhW)r}r(hXAs in the previous cases, you can pass parameters to the method as well by including them in the array that specifies the callback:rh jh!h$h&h[h(}r(h.]rh0]rh,]rh*]rh2]ruh5Kh6hh]rhSXAs in the previous cases, you can pass parameters to the method as well by including them in the array that specifies the callback:rr}r (hjh jubaubj)r }r (hXAMathJax.Ajax.Require("filename",["method",object,arg1,arg2,...]);r h jh!h$h&jh(}r (jjX javascriptrjjh*]rh,]rh.]rh0]rh2]ruh5Kh6hh]rhSXAMathJax.Ajax.Require("filename",["method",object,arg1,arg2,...]);rr}r(hUh j ubaubhW)r}r(hXThis approach is useful when you are pushing a callback for one one MathJax's Hub routines into the MathJax processing queue. For example,rh jh!h$h&h[h(}r(h.]rh0]rh,]rh*]rh2]r uh5Kh6hh]r!hSXThis approach is useful when you are pushing a callback for one one MathJax's Hub routines into the MathJax processing queue. For example,r"r#}r$(hjh jubaubj)r%}r&(hX5MathJax.Hub.Queue(["Typeset",MathJax.Hub,"MathDiv"]);r'h jh!h$h&jh(}r((jjX javascriptr)jjh*]r*h,]r+h.]r,h0]r-h2]r.uh5Kh6hh]r/hSX5MathJax.Hub.Queue(["Typeset",MathJax.Hub,"MathDiv"]);r0r1}r2(hUh j%ubaubhW)r3}r4(hXVpushes the equivalent of ``MathJax.Hub.Typeset("MathDiv")`` into the processing queue.r5h jh!h$h&h[h(}r6(h.]r7h0]r8h,]r9h*]r:h2]r;uh5Kh6hh]r<(hSXpushes the equivalent of r=r>}r?(hXpushes the equivalent of r@h j3ubjD)rA}rB(hX"``MathJax.Hub.Typeset("MathDiv")``rCh(}rD(h.]rEh0]rFh,]rGh*]rHh2]rIuh j3h]rJhSXMathJax.Hub.Typeset("MathDiv")rKrL}rM(hUh jAubah&jRubhSX into the processing queue.rNrO}rP(hX into the processing queue.rQh j3ubeubhW)rR}rS(hXSee the :ref:`Callback Object ` reference pages for more information about the valid methods of specifying a callback.rTh jh!h$h&h[h(}rU(h.]rVh0]rWh,]rXh*]rYh2]rZuh5Kh6hh]r[(hSXSee the r\r]}r^(hXSee the r_h jRubh)r`}ra(hX%:ref:`Callback Object `rbh jRh&hh(}rc(UreftyperdXrefrehX api-callbackrfU refdomainrgXstdrhh*]rih,]rjU refexplicitrkh.]rlh0]rmh2]rnhhuh5Kh]roh)rp}rq(hjbh(}rr(h.]rsh0]rt(hjhXstd-refrueh,]rvh*]rwh2]rxuh j`h]ryhSXCallback Objectrzr{}r|(hUh jpubah&hubaubhSXW reference pages for more information about the valid methods of specifying a callback.r}r~}r(hXW reference pages for more information about the valid methods of specifying a callback.rh jRubeubeubh8)r}r(hUh jh!h$h&h=h(}r(h.]rh0]rh,]rh*]rhah2]rh auh5Kh6hh]r(hG)r}r(hXCreating a Callback Explicitlyrh jh!h$h&hKh(}r(h.]rh0]rh,]rh*]rh2]ruh5Kh6hh]rhSXCreating a Callback Explicitlyrr}r(hjh jubaubhW)r}r(hX:When you call a method that accpets a callback, you usually pass it a callback specification (like in the examples above), which *describes* a callback (the method will create the actual `Callback` object, and return that to you as its return value). You don't usually create `Callback` objects directly yourself.rh jh!h$h&h[h(}r(h.]rh0]rh,]rh*]rh2]ruh5Kh6hh]r(hSXWhen you call a method that accpets a callback, you usually pass it a callback specification (like in the examples above), which rr}r(hXWhen you call a method that accpets a callback, you usually pass it a callback specification (like in the examples above), which rh jubh)r}r(hX *describes*rh(}r(h.]rh0]rh,]rh*]rh2]ruh jh]rhSX describesrr}r(hUh jubah&hubhSX/ a callback (the method will create the actual rr}r(hX/ a callback (the method will create the actual rh jubhg)r}r(hX `Callback`rh(}r(h.]rh0]rh,]rh*]rh2]ruh jh]rhSXCallbackrr}r(hUh jubah&huubhSXP object, and return that to you as its return value). You don't usually create rr}r(hXP object, and return that to you as its return value). You don't usually create rh jubhg)r}r(hX `Callback`rh(}r(h.]rh0]rh,]rh*]rh2]ruh jh]rhSXCallbackrr}r(hUh jubah&huubhSX objects directly yourself.rr}r(hX objects directly yourself.rh jubeubhW)r}r(hX6There are times, however, when you may wish to create a callback object for use with functions that don't create callbacks for you. For example, the ``setTimeout()`` function can take a function as its argument, and you may want that function to be a method of an object, and would run into the problem described in the previous section if you simply passed the object's method to ``setTimeout()``. Or you might want to pass an argument to the function called by ``setTimeout()``. (Altough the ``setTimeout()`` function can accept additional arguements that are supposed to be passed on to the code when it is called, Internet Explorer does not implement that feature, so you can't rely on it.) You can use a `Callback` object to do this, and the :meth:`MathJax.Callback()` method will create one for you. For example,rh jh!h$h&h[h(}r(h.]rh0]rh,]rh*]rh2]ruh5Kh6hh]r(hSXThere are times, however, when you may wish to create a callback object for use with functions that don't create callbacks for you. For example, the rr}r(hXThere are times, however, when you may wish to create a callback object for use with functions that don't create callbacks for you. For example, the rh jubjD)r}r(hX``setTimeout()``rh(}r(h.]rh0]rh,]rh*]rh2]ruh jh]rhSX setTimeout()rr}r(hUh jubah&jRubhSX function can take a function as its argument, and you may want that function to be a method of an object, and would run into the problem described in the previous section if you simply passed the object's method to rr}r(hX function can take a function as its argument, and you may want that function to be a method of an object, and would run into the problem described in the previous section if you simply passed the object's method to rh jubjD)r}r(hX``setTimeout()``rh(}r(h.]rh0]rh,]rh*]rh2]ruh jh]rhSX setTimeout()rr}r(hUh jubah&jRubhSXC. Or you might want to pass an argument to the function called by rr}r(hXC. Or you might want to pass an argument to the function called by rh jubjD)r}r (hX``setTimeout()``r h(}r (h.]r h0]r h,]rh*]rh2]ruh jh]rhSX setTimeout()rr}r(hUh jubah&jRubhSX. (Altough the rr}r(hX. (Altough the rh jubjD)r}r(hX``setTimeout()``rh(}r(h.]rh0]rh,]rh*]r h2]r!uh jh]r"hSX setTimeout()r#r$}r%(hUh jubah&jRubhSX function can accept additional arguements that are supposed to be passed on to the code when it is called, Internet Explorer does not implement that feature, so you can't rely on it.) You can use a r&r'}r((hX function can accept additional arguements that are supposed to be passed on to the code when it is called, Internet Explorer does not implement that feature, so you can't rely on it.) You can use a r)h jubhg)r*}r+(hX `Callback`r,h(}r-(h.]r.h0]r/h,]r0h*]r1h2]r2uh jh]r3hSXCallbackr4r5}r6(hUh j*ubah&huubhSX object to do this, and the r7r8}r9(hX object to do this, and the r:h jubh)r;}r<(hX:meth:`MathJax.Callback()`r=h jh&hh(}r>(Ureftyper?Xmethr@hXMathJax.CallbackrAU refdomainrBXpyrCh*]rDh,]rEU refexplicitrFh.]rGh0]rHh2]rIhhjANjBNuh5Kh]rJjD)rK}rL(hj=h(}rM(h.]rNh0]rO(hjCXpy-methrPeh,]rQh*]rRh2]rSuh j;h]rThSXMathJax.Callback()rUrV}rW(hUh jKubah&jRubaubhSX. method will create one for you. For example,rXrY}rZ(hX. method will create one for you. For example,r[h jubeubj)r\}r](hX\function myTimer (x) {alert("x = "+x)} setTimeout(MathJax.Callback([f,"Hello World!"]),500);r^h jh!h$h&jh(}r_(jjX javascriptr`jjh*]rah,]rbh.]rch0]rdh2]reuh5Kh6hh]rfhSX\function myTimer (x) {alert("x = "+x)} setTimeout(MathJax.Callback([f,"Hello World!"]),500);rgrh}ri(hUh j\ubaubhW)rj}rk(hXiwould create a callback that calls ``f("Hello World!")``, and schedules it to be called in half a second.rlh jh!h$h&h[h(}rm(h.]rnh0]roh,]rph*]rqh2]rruh5Kh6hh]rs(hSX#would create a callback that calls rtru}rv(hX#would create a callback that calls rwh jjubjD)rx}ry(hX``f("Hello World!")``rzh(}r{(h.]r|h0]r}h,]r~h*]rh2]ruh jjh]rhSXf("Hello World!")rr}r(hUh jxubah&jRubhSX1, and schedules it to be called in half a second.rr}r(hX1, and schedules it to be called in half a second.rh jjubeubeubeubeubehUU transformerrNU footnote_refsr}rUrefnamesr}rUsymbol_footnotesr]rUautofootnote_refsr]rUsymbol_footnote_refsr]rU citationsr]rh6hU current_linerNUtransform_messagesr]rcdocutils.nodes system_message r)r}r(hUh(}r(h.]rUlevelrKh*]rh,]rUsourcerh$h0]rh2]rUlinerKUtyperUINFOruh]rhW)r}r(hUh(}r(h.]rh0]rh,]rh*]rh2]ruh jh]rhSX5Hyperlink target "using-callbacks" is not referenced.rr}r(hUh jubah&h[ubah&Usystem_messagerubaUreporterrNUid_startrKU autofootnotesr]rU citation_refsr}rUindirect_targetsr]rUsettingsr(cdocutils.frontend Values ror}r(Ufootnote_backlinksrKUrecord_dependenciesrNU rfc_base_urlrUhttp://tools.ietf.org/html/rU tracebackrKUpep_referencesrNUstrip_commentsrNU toc_backlinksrUentryrU language_coderUenrU datestamprNU report_levelrKU _destinationrNU halt_levelrKU strip_classesrNhKNUerror_encoding_error_handlerrUbackslashreplacerUdebugrNUembed_stylesheetrUoutput_encoding_error_handlerrUstrictrU sectnum_xformrKUdump_transformsrNU docinfo_xformrKUwarning_streamrNUpep_file_url_templaterUpep-%04drUexit_status_levelrKUconfigrNUstrict_visitorrNUcloak_email_addressesrUtrim_footnote_reference_spacerUenvrNUdump_pseudo_xmlrNUexpose_internalsrNUsectsubtitle_xformrU source_linkrNUrfc_referencesrNUoutput_encodingrUutf-8rU source_urlrNUinput_encodingrU utf-8-sigrU_disable_configrNU id_prefixrUU tab_widthrKUerror_encodingrUUTF-8rU_sourcerUS/Data/Code/JavaScript/MathJax/Code/SourceForge/trunk/mathjax/docs/rst/callbacks.rstrU generatorrNUdump_internalsrNU pep_base_urlrUhttp://www.python.org/dev/peps/rUinput_encoding_error_handlerrjUauto_id_prefixrUidrUdoctitle_xformrUstrip_elements_with_classesrNU _config_filesr]rUfile_insertion_enabledrKU raw_enabledrKU dump_settingsrNubUsymbol_footnote_startrKUidsr}r(hh9hjhjhjhh9uUsubstitution_namesr}rh&h6h(}r(h.]rh*]r h,]r Usourcer h$h0]r h2]r uU footnotesr]rUrefidsr}rh]rhasub.