Spent time reading the answers here which helped me understand the asynchronous nature of JavaScript a little better. I'm still getting my feet wet. I'm trying to create my own callbacks by bindings setTimeouts
to different events. Specifically change
and click
. I'm clearly missing something. When I select a year, I get below error in the console. Any help? Thanks!
html
<select id="select_year">
<option value="2021">2021</option>
<option value="2022">2022</option>
<option value="2023">2023</option>
</select>
js
function selectYear(selectedYear) {
$(document).on('change','#select_year',setTimeout(function() {
selectedYear = $(this).val();
}, 0),
)}
selectYear(function(year) {
console.log(year);
});
error in console
Uncaught TypeError: t.nodeName is undefined
val jQuery
selectYear http://localhost:8080/js/calendar.js:20
setTimeout handler*selectYear http://localhost:8080/js/calendar.js:18
<anonymous> http://localhost:8080/js/calendar.js:25
jquery.min.js:2:69132
val jQuery
selectYear http://localhost:8080/js/calendar.js:20
(Async: setTimeout handler)
selectYear http://localhost:8080/js/calendar.js:18
<anonymous> http://localhost:8080/js/calendar.js:25