Can someone explain Javascript Asynchronous Callbacks? [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm rather new to Javascript and some of the syntax is not very clear to me. I've usually copied example code from the internet when I needed it, but I would like to understand it better.
Can someone explain, in simple terms, how javascript knows to call the function supplied, and how it knows to do it asynchronously? Is there something that tells it do do this, or is it just built into the language?
Thanks

Whenever you want to something when something happens (and that something is outside the control of JavaScript (e.g. "When the user clicks on a button" or "When the HTTP response is received from the network".)) you typically use an Event Listener.
This is a function that you tell JavaScript to run when the event happens.
They are typically set up by using the addEventListener method or by assigning a function to a property with a predefined name on the right kind of object.
When the event happens some code (typically (in a browser this is almost always) provided by the underlying environment) will check to see if any appropriate event listener functions exist and executes them.

Related

what is meaning of "on" [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I am a Japanese web developer.
I am not good at English , but I want to be better.
Sometimes, I see function starts with "on" , like "onResize" , "onDrop".
What is this "on" meaning?
I thought this "on**" means like "when ** ( happened )".
So, I thought it would be better to say "onResized" , not "onResize".
Or "onWindowResized" , not "onWindowResize".
Can someone please tell me , what is the meaning of "on"?
Thank you so much for a lot of answer.
I read them all.
Well, what I thought is that if you say "onWindowResize" I feel like the window resizes them self by own.
But the one who resizes the window is us.
And window is something that is resized by us.
So I feel weird to hear "onWindowResize".
Well but I am bad at English , and my feeling of English should be wrong.
For no confusion, You can easily think on as it is being happening, not always the completion.
onChange for example, something is being changing(event) so we might add some event-handler for that event.
For your edited question, It doesn't matter that who resizes the window.
You can focus it to event - event-handler relation.
When someone (maybe your user) changes the window size using his/her mouse, you can add some event-handler like alert("window is being resized") to onWindowResize event which means window resizing is being happened.
It works like the English phrases "dead on arrival" or "cash on delivery". Arrival and delivery represent events in time, so in javascript it is like saying "take action on event", where the action is the associated function and the event is the javascript event.
Using "on" before any function, its just a convention. As an example, in react most of the developers define their function with two ways -
handleResize
resizeHandler
I think, on indicates - this function is doing/did/will do something according to any event.
Like you want to remove a div when clicking a button, you can define this function like onRemove.
When it say on[Verb] it mean when [Verb] happened or being happen: not always after happened.

JavaScript Event Flow - When would you use it. Real World Example [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
When would you ever use the Event Flow is an application? What I'm referring to is the third parameter of the AddEventListener function. Can anyone provide a real world example?
There aren't a lot of reasons to use event capturing instead of event bubbling. Furthermore, event capturing isn't supported in IE8.
For more info, this page deals with the differences between bubbling/capturing.
In practice, the only reason that I can think of to use event capturing is to deal with events that don't bubble, namely onfocus and onblur. See also this SO post dealing with onblur

Anonymous function vs Named function which is better and why [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I search on google but did not find clear answer about anonymous and named function. I am looking for simple answer which one is better any why or its depends on requirement. So I am looking forward to your valuable answer about these topic. Your answer is really help me out to understand this. Thanks in advance
The advantages of a named function (expression) are:
makes it more reliable to call the function recursively, since the name becomes a binding inside the function itself.
can create a better call stack (by using the function name instead of <anonymous>
Using a named function (expression) might not be possible if
you care about IE6, which doesn't handle them properly (it creates two functions)
you can't think of a name that wouldn't shadow a variable you need to access inside the function

First time writing Javascript properly [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I've been writing Javascript with jQuery for a while, I can make it do what I want, but I've never written anything really reusable or modular. Now it's time for me to take that step and write proper reusable Javascript.
I thought I'd start with something I've implemented countless times, a confirm delete dialog. I want to be able to specify a function to execute on confirm and a function to execute on cancel.
The way I see this working (and this is open to criticism) is to do something like:
$(element).confirmDialog(function(){
// this is the cancel callback
},
function(){
// this is the confirm callback
});
I'd also like the dialog to show based on a data attribute on the link, rather than having to write an .on('click'... handler each time, but I don't know how to 'link' the specific confirmDialog with the function which handles the .on('click'....
This is really as far as I've got so far. I know that as I want to be able to add the functionality to any element I need to define confirmDialog() as $.fn.confirmDialog = function(){...}.
Although I can implement the entire thing in an ad-hoc way, I'm unsure as to how to implement this functionality as a clearly defined, loosely coupled reusable module.
Could someone help me get my head around how to structure this module, or provide a link to a very thorough tutorial which is specifically about writing reusable Javascript?
You can read more about how to create jQuery plugins at the following links:
http://learn.jquery.com/plugins/basic-plugin-creation/
http://www.codeproject.com/Articles/291290/How-To-Write-Plugin-in-jQuery
NetTuts videos are particularly useful:
http://net.tutsplus.com/articles/news/learn-how-to-create-a-jquery-plugin/

Defining functions in javascript/jQuery [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I had this, and it was working and all was good.
I then tried to make the change to this, and it all went wrong.
Am I defining functions in the wrong way? Sorry, I'm new to JS & jQ (Though I'm assuming this is just basic javascript rather than anything jQuery-related)
EDIT: Also, does the MAILTO: href not work? I tried it and didn't receive an email... anyway, thanks!
You should change these:
$("#name").focusout(checkName());
to
$("#name").focusout(checkName);
You want to assign the function itself as a focusout handler, but instead you're assigning whatever the function returns as a handler.
Also, the mailto: URL scheme does not send emails. It opens whatever local mail client is defined as handler for this scheme on the visitor's machine and commences composition of a new message addressed to the address in the URL.
Pass the function name, not the result of the function executed.
$("#name").focusout(checkName);
$("#pass").focusout(checkPass);
$("#pass2").focusout(checkPass2);
Add var infront of your function names (not completely necessary but stops them being global):
var checkName = function(){
And then pass them to the event binding functions without the () as this is running the function and passing the result rather than passing the actual function itself:
$("#name").focusout(checkName);
This should fix the problem.
http://jsfiddle.net/infernalbadger/Hxnv5/6/
Also, the mailto: link works fine. It should open up your default email client.

Categories