If so, can someone provide example code?
Thanks!
Jerry,
I use dojo on.once quit often in code.
on<SPAN class="punctuation token">.</SPAN><SPAN class="token function">once</SPAN><SPAN class="punctuation token">(</SPAN>map<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"update-end"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">function</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">{</SPAN> <SPAN class="comment token">// will only fire once...</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Hello Jerry,
I am not able to understand the objective of a example. on.once is a method in dojo, used to listen to an event only once. it would be similar to code below.
<SPAN class="keyword token">var</SPAN> handle <SPAN class="operator token">=</SPAN> <SPAN class="token function">on</SPAN><SPAN class="punctuation token">(</SPAN>map<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"click"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">function</SPAN><SPAN class="punctuation token">(</SPAN>evt<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">{</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> handle<SPAN class="punctuation token">.</SPAN><SPAN class="token function">remove</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
It does not matter, which event. you can use on.once on any event, which you would like to listen just once.
Thanks. But, it is not always that simple... I want my code to fire only once. I have a couple callbacks when the event occurs. So... I need to wait until the callbacks are finished or, I was thinking, use the nifty on.once dojo functionality. I'm still not clear how to invoke it's magic.
None of the following works:
on.once(...);
on(...).once();
handle.once();
I see. Pretty simple. Thanks!
This works as long as the dojo.on module is called first, like so:
on<SPAN class="" style="border: 0px; font-weight: inherit;">.</SPAN><SPAN class="" style="color: #d74444; border: 0px; font-weight: inherit;">once</SPAN><SPAN class="" style="border: 0px; font-weight: inherit;">(</SPAN>map<SPAN class="" style="border: 0px; font-weight: inherit;">,</SPAN> <SPAN class="" style="color: #669900; border: 0px; font-weight: inherit;">"update-end"</SPAN><SPAN class="" style="border: 0px; font-weight: inherit;">,</SPAN> <SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">function</SPAN><SPAN class="" style="border: 0px; font-weight: inherit;">(</SPAN><SPAN class="" style="border: 0px; font-weight: inherit;">)</SPAN><SPAN class="" style="border: 0px; font-weight: inherit;">{</SPAN> <SPAN class="" style="color: slategray; border: 0px; font-weight: inherit;">// will only fire once...</SPAN> <SPAN class="" style="border: 0px; font-weight: inherit;">}</SPAN><SPAN class="" style="border: 0px; font-weight: inherit;">)</SPAN><SPAN class="" style="border: 0px; font-weight: inherit;">;</SPAN><CODE><SPAN style="color: #3d3d3d;">require(["dojo/on"], </SPAN>function(on) {
on<SPAN class="" style="border: 0px; font-weight: inherit;">.</SPAN><SPAN class="" style="color: #d74444; border: 0px; font-weight: inherit;">once</SPAN><SPAN class="" style="border: 0px; font-weight: inherit;">(</SPAN>map<SPAN class="" style="border: 0px; font-weight: inherit;">,</SPAN> <SPAN class="" style="color: #669900; border: 0px; font-weight: inherit;">"update-end"</SPAN><SPAN class="" style="border: 0px; font-weight: inherit;">,</SPAN> <SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">function</SPAN><SPAN class="" style="border: 0px; font-weight: inherit;">(</SPAN><SPAN class="" style="border: 0px; font-weight: inherit;">)</SPAN><SPAN class="" style="border: 0px; font-weight: inherit;">{</SPAN> <SPAN class="" style="color: slategray; border: 0px; font-weight: inherit;">// will only fire once...</SPAN> <SPAN class="" style="border: 0px; font-weight: inherit;">}</SPAN><SPAN class="" style="border: 0px; font-weight: inherit;">)</SPAN><SPAN class="" style="border: 0px; font-weight: inherit;">;</SPAN>
<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">});</SPAN>
Thanks for this great tip!
Přihlášení členové mohou přispívat, sledovat aktualizace a další. Jste tu noví? Zaregistrujte si bezplatný účet.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.