Life coding javascript ajax example question

Asked 2 years ago, Updated 2 years ago, 68 views

Hello, I'm leaving a question for the first time

I'm understanding Ajax communication while watching life coding courses

JavaScript - Ajax (2/3) : Basic Method Course

https://opentutorials.org/module/904/6843

I have a question in the example shown in

demo1.html

<p>time : <span id="time"></span></p>
<input type="button" id="execute" value="execute" />
<script>
document.querySelector('input').addEventListener('click', function(event){
    var xhr = new XMLHttpRequest();
    xhr.open('GET', './time.php');
    xhr.onreadystatechange = function(){
        if(xhr.readyState === 4 && xhr.status === 200){
            document.querySelector('#time').innerHTML = xhr.responseText;
        }
    }
    xhr.send(); 
}); 
</script> 

time.php

<?php
$d1 = new DateTime;
$d1->setTimezone(new DateTimezone("asia/seoul"));
echo $d1->format('H:i:s');
?>

demo time in html. 1.Php that prints the contents of the example.

What I'm curious about is The factor of the function below in the ajax code in demo1.html

function(event) Event factors in the function are

time.Output from php echo $d1->format('H:i:s');

Do I take over the price?

Or 'click' Is there an order to receive the event and execute the function?

Thank you ^ ^ ^

javascript ajax php

2022-09-21 20:10

2 Answers

The event factor is an event object. When a DOM-related event occurs (click here), all relevant information is stored in an object called event. I handed over an event handler called function(event) {~~} to addEventListener, and when that event occurs in my browser, I run the event handler. In conclusion, the event factor is that the browser calls the event handler with the event object when an HTML event occurs.

Note that the resulting value of php is contained in xhr.responseText;


2022-09-21 20:10

addEventListener( 'eventType', fn )

addEventListener is a method of registering a function to be called when an event matching 'eventType' occurs. A function called is usually called an event handler. By default, when an event handler is called, the first forwarding factor passes objects that contain information about the event (such as what the event occurred, event type, and so on).

The subject that invokes the event handler is a browser that is a JavaScript implementation (or launcher).

Related documents:

And the code below is:

<script>
document.querySelector('input').addEventListener('click', function(event) {
    console.log('do something');
});
</script>

It's as below:

<script>
function myFunction(event){
    console.log('do something');
}
document.querySelector('input').addEventListener('click', myFunction);
// Click When an event occurs, call myFunction and pass the event object to the first factor
</script>


2022-09-21 20:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.