HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URL's added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div class="container">
<div class="row no-gutters"id="header-row">
<div class="col-sm-2">
<h2>You used to call me....</h2>
</div> <!-- end of col-sm-4 -->
<div class="col-sm-8">
<img src="https://gph.is/2JKqoxO" id="header-pic">
</div> <!-- end of col- sm- 4 -->
<div class="col-sm-2">
<h2>Callback functions in JavaScript</h2>
</div> <!-- end of col-sm-4 -->
</div><!-- end of row -->
<div class="row">
<div class="col-sm-6">
<h2>Background</h2>
<p>JS is considered to be synchronous i.e. single threaded. So one line of code is executed at a time. It cannot multi-task and move on to the next line/block of code till the current line/block has been executed.</p>
<p>So if you have a JavaScript function which has to perform some complex computations, it may well be possible that during that time other parts of the application freeze..</p>
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQQEBAQEBMQEBAQEBAPEBAPFRIVDw8QFRUWFhYVFRYYICggGhoxHRUTIzIhJikrLy4uGCAzOjMtNyguOisBCgoKDg0OFxAPGy0dHyUtLy0tLS0xLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSstLS0tLS0tLS0tLS0tLf/AABEIAKEBOQMBIgACEQEDEQH/xAAbAAABBQEBAAAAAAAAAAAAAAAAAQMEBQYHAv/EAEoQAAIAAwMFDAYHBwMEAwAAAAECAAMRBBIhBRMxUZEUFRYiQVNhkpPR0tMGIzJSVHMzQmNxgbLiNENigrGz4TWhoiREcsElwvD/xAAaAQEBAQEBAQEAAAAAAAAAAAAAAQIDBAUG/8QAKhEBAAEDAgQGAgMBAAAAAAAAAAECERIDUQQUUqETFSExQWEiMgWB8LH/2gAMAwEAAhEDEQA/AOf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ggCCCCAIIIIAggggCCCCAIIIIAggggCCCCAIIIIAggggCCCCAIIIIAggggCCCCAIxEEEB//2Q==">
<p>If you're reading this then you are probably acquainted with the idea that almost everything in JS is an object (primitive types behave as objects as they have an object wrapper class around them). This includes functions.</p>
<p>So functions are objects i.e the typeof is object,which means:
<ul>
<li>Functions can be stored in variables</li>
<li>You can pass in a function as an argument to another function</li>
<li>You can return a function, from a function</li>
<li>One function can be created inside another</li>
<p><i>These are aspects of what is known as asychronous programming</i></p>
</ul>
</p>
<p><b>Some use cases of callback functions</b></p>
<ul>
<li>DOM events sucha s clicks and User Input/Output</li>
<li>Databases (e.g. MySQL,MongoDB,CouchDB etc)</li>
<li> Fetching data from APIs (e.g. Facebook, Twitter, Push Notifications)</li>
<li>HTTP and web socket connections</li>
</ul>
</div> <!-- end of col-sm-6 -->
<div class="col-sm-6">
<h2>Callback functions</h2>
<p><mark>A function that is passed as an argument to another function to be called, is called a Callback function</mark></p>
<p>The function that accepts another function as an argument is called a higher-order function and contains the logic for when the callback function is executed. This outer function undertakes the async task.</p>
<p><q>A reference to executable code, or a piece of executable code, that is passed as an argument to other code. - Wikipedia</q></p>
<p>But why?</p>
<p><mark>Callback functions are efficient as a function doesn't have to wait for something to finish/return a value in order to complete a routine/task</mark>These are often used in DOM events.</p>
<p></p>
<p><i>Callback functions can be asynchronous or synchronous.
</i></p>
<p>In a normal function some value is returned and stored in a variable to presumably perform some other task. However, with callback functions the callback funtion is passed as an argument to the main function and, the value returned from the main function is provided instead to the callback function.</p>
<p>It is also a way of making sure that an event for example "click" has taken place before something else runs.</p>
<p>So the callback function is available but won't run till an event for example "click" event occurs</p>
<p>Example:</p>
<code>
<span class="comment">//mainFunc accepts a callback function as a parameter</span><br>
function mainFunc(item1, item2, callback) {<br>
alert('I like eating: ' + item1 + 'and ' + item2);<br>
<span class="comment">//callback function is called after the above lines have finished executing</span><br>
callback();<br>
}<br>
mainFunc('chocolate', 'cheese', function() {<br>
alert('Do you have some?');<br>
});<br>
</code>
</div> <!-- end of col-sm-6 -->
</div> <!-- end of row -->
<div class='row'>
<div class="col-sm-6">
<h2>What's happening?</h2>
<p>When we pass a function as an argument to another function, we are treating it like a variable. </p>
<p>Only the function defination is passed. The function is not executed in the parameter.</p>
<p>The Callback function is executed at some point later within the original function's body.</p>
<p> JavaScript is still executing the code/functions in the order given. The only change is that JS doesn't wait for a response before moving on.</p>
</div> <!-- end of col-sm-6 -->
<div class="col-sm-6">
<h2>How</h2>
<ol>
<li><p>Pass a named function to be called immediately after the main function. Named functions don't have '( )' when passed as a function parameter to another function</p>
</li>
<li><p>Anonymous function as a parameter to another function </p>
</li>
<li> You can store a reference to the callback function in a named variable </li>
</ol>
</div> <!-- end of col-sm-6 -->
</div> <!-- end of row -->
<div class="row">
<div class="col-md-6">
<h2>Exercise</h2>
<p>1. Code a callback function which runs upon a click event on the button below to alert the string "hello"</p>
<p>2. Code a callback function that will alert the string "bye" 5 seconds after the callback function following the click event</p>
<div class='row'>
<div class="col-sm-6">
<img src ="https://media.giphy.com/media/l378hTn2fvdjSNhew/giphy.gif">
</div> <!-- end of nested col-6 -->
<div class="col-sm-6">
<p> This is the button to work with. It has an id of "myButton" via which you can select it in your JS file or here on codepen </p>
<p><button class="btn btn-success btn-lg" id="myButton">Button</button></p>
<p><i class="fa fa-arrow-down fa-3x"></i></p>
<p>Code a function called which is called after the click event. The function uses an anonymous function as a callback and alerts ("This is alerted 2nd").
<p><i class="fa fa-arrow-down fa-3x"></i></p>
<p>Code a function called <mark>"callBackFunction"</mark> which is referenced by the variable <code>var callbackFuncVar</code> and is called after the click event and alerts("bye")</p>
<p>Call the callback function referenced by var callbackFuncVar within this main function</p></p>
</div> <!-- end of nested col-6 -->
</div> <!-- end of nested row -->
</div><!-- end of col-sm-6 -->
<div class="col-md-6">
<h2>Examples:</h2>
<ol>
<li> <p>Named Callback function</p>
<code><button id="myId">click</button><br>
var button = document.getElementById("myButton");
button.addEventListener("click");
button.addEventListener("click", hello());
<br>
function hello(){ <br>
alert("hello"); <br>
}
</code>
</li>
<li> <p>AnonymousCallback function</p><code>
button.addEventListener("click", function(){<br>
alert("hello");<br>
});
</code></li>
<li> <p>Using a named variable</p><code>
function addNum(num1, num2 , callback){ </br>
heading.innerHTML+= ( "This is displayed first" + num1 + num2); <br>
callback(); <br>
} </br>
<span class="comment"> // display() function is called just after the ending of addNum()</span> <br>
var display = function display(){ <br>
heading.innerHTML+= ("This is printed after addition"); <br>
} <br>
<span class="comment">// Calling addNum()</span><br>
function
addNum(1,6,display);
</code>
</li>
</ol>
</div><!-- end of col-sm-6 -->
</div><!-- end of row -->
<div class="row">
<div class="col-sm-4">
<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">JavaScript defines a `callback` differently than other languages. Callbacks typically happens afterwards. In JavaScript this is not the case. callback can refer to a function pointer and be synchronous. I thought I saw this from <a href="https://twitter.com/rauschma?ref_src=twsrc%5Etfw">@rauschma</a>, but I can't find it now.</p>— JavaScript Joel 👾 (@joelnet) <a href="https://twitter.com/joelnet/status/1120792338165420032?ref_src=twsrc%5Etfw">April 23, 2019</a></blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
</div><!-- end of col-sm-4 -->
<div class="col-sm-4">
<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">Day 6 <a href="https://twitter.com/hashtag/100DaysOfCode?src=hash&ref_src=twsrc%5Etfw">#100DaysOfCode</a> Forgot to tweet yesterday. <br><br>Learned about the DOM (Document Object Model) and callback functions. A callback function is a function that we pass into another function as an argument, which will then call the function for us. <a href="https://twitter.com/hashtag/javascript?src=hash&ref_src=twsrc%5Etfw">#javascript</a> <a href="https://twitter.com/hashtag/ES6?src=hash&ref_src=twsrc%5Etfw">#ES6</a></p>— Ian (@iandv6) <a href="https://twitter.com/iandv6/status/1104976471402065921?ref_src=twsrc%5Etfw">March 11, 2019</a></blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">My answer to How does the Javascript callback function work? <a href="https://t.co/KzGci2jf34">https://t.co/KzGci2jf34</a></p>— Neha Sharma (@hellonehha) <a href="https://twitter.com/hellonehha/status/1118450069177602048?ref_src=twsrc%5Etfw">April 17, 2019</a></blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
</div>
<div class="col-sm-4">
<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">Javascript Callback Function Example <a href="https://t.co/BwlbTsjYhl">https://t.co/BwlbTsjYhl</a></p>— Web Code Geeks (@webcodegeeks) <a href="https://twitter.com/webcodegeeks/status/1121428678888579075?ref_src=twsrc%5Etfw">April 25, 2019</a></blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">Two JavaScript functions on the phone:<br><br>— "Don't worry, I'll give you a callback."<br>— "Promise?"</p>— Eduardo Bouças (@eduardoboucas) <a href="https://twitter.com/eduardoboucas/status/627807319687081984?ref_src=twsrc%5Etfw">August 2, 2015</a></blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
</div><!-- end of col-sm-4 -->
</div> <!-- end of row -->
<footer>
<p>Code worksheet #4. Made with <i class="fa fa-heart-o"></i> and <i class="fa fa-coffee"></i> Kauress 2019</p>
</footer>
</div> <!-- end of container -->
@import url("https://fonts.googleapis.com/css?family=Montserrat:400,400i,700");
body{
font-family: Montserrat, sans-serif;
overflow-x: hidden;
}
h2{
margin-top: 30px;
text-align:left;
}
#header-row{
text-align:center;
}
#header-pic{
height: 250px;
}
footer{
margin-top:40px;
text-align: center;
}
mark{
background-color: yellow;
}
.fa-arrow-down {
margin: 10px;
}
.comment{
background-color: #D8D8D8;
}
code{
font-size: 15px;
}
Also see: Tab Triggers