
progress
16 March, 2023
0
0
0
Contributors
About
The ProgressEvent
interface is used to handle events that represent the progress of an operation, such as loading a resource, uploading a file, or downloading a large amount of data. The ProgressEvent
provides information about the progress of the operation, including the total size of the resource, the amount of data that has been transferred, and the current state of the operation.
The ProgressEvent
interface is commonly used in applications to provide feedback to the user about the progress of long-running operations. For example, when uploading a large file, the user may want to know how much of the file has been uploaded and how long it will take to complete the upload. The ProgressEvent
interface can be used to display a progress bar or other visual indicator to the user to provide feedback on the progress of the operation.
In addition to providing information about the progress of an operation, the ProgressEvent
interface also allows developers to handle errors and abort events. If an error occurs during an operation, the error
event is triggered and can be handled by the application. Similarly, if the operation is aborted, the abort
event is triggered and can be handled by the application.
Event listener
Here's an example of using the ProgressEvent
interface to display the progress of uploading a file using the XMLHttpRequest
object:
// Get the file input element from the HTML document.
const fileInput = document.getElementById('file-input');
// Add an event listener to the file input element to listen for changes.
fileInput.addEventListener('change', () => {
// Create a new XMLHttpRequest object.
const xhr = new XMLHttpRequest();
// Set up the event listeners for the XMLHttpRequest object.
xhr.upload.addEventListener('progress', handleUploadProgress);
xhr.upload.addEventListener('error', handleUploadError);
xhr.upload.addEventListener('abort', handleUploadAbort);
// Open the XMLHttpRequest object and specify the URL to send the data to.
xhr.open('POST', '/upload');
// Send the file data as a FormData object.
const formData = new FormData();
formData.append('file', fileInput.files[0]);
xhr.send(formData);
});
// Define a function to handle the upload progress event.
function handleUploadProgress(event) {
if (event.lengthComputable) {
const percentComplete = Math.round((event.loaded / event.total) * 100);
console.log(`Upload progress: ${percentComplete}%`);
}
}
// Define a function to handle the upload error event.
function handleUploadError(event) {
console.error('Upload error');
}
// Define a function to handle the upload abort event.
function handleUploadAbort(event) {
console.warn('Upload aborted');
}
Here, we add an event listener to the file input element to listen for changes. When a file is selected, we create a new XMLHttpRequest
object and set up event listeners for the progress
, error
, and abort
events. We then open the XMLHttpRequest
object and send the file data as a FormData
object.
When the progress
event is fired, we call the handleUploadProgress
function to handle the event. This function checks if the total size of the resource is computable, and if so, calculates the percentage of the resource that has been loaded and logs it to the console. We also define handleUploadError
and handleUploadAbort
functions to handle the error
and abort
events, respectively.
By using the ProgressEvent
interface, we can provide feedback to the user about the progress of the file upload operation, including the percentage of the file that has been uploaded.
Property
Here's an example of using the onprogress
property to handle progress events:
const image = document.createElement('img');
image.src = 'image.jpg';
image.onprogress = function(event) {
if (event.lengthComputable) {
const percentComplete = Math.round((event.loaded / event.total) * 100);
console.log(`Image loading progress: ${percentComplete}%`);
}
};
document.body.appendChild(image);
Here, we create a new img
element and set its src
attribute to the URL of an image file. We then set the onprogress
property of the img
element to a function that handles the progress
event.
In the event handler function, we check if the total size of the resource is computable using the lengthComputable
property of the event object. If it is, we calculate the percentage of the resource that has been loaded and log it to the console.
Finally, we append the img
element to the body of the HTML document to initiate the loading of the image. As the image loads, the onprogress
event handler will be called periodically to update the progress of the operation.
Inline
Here's an example of using the onprogress
attribute inline in HTML:
HTML
<img src="image.jpg" onprogress="handleImageProgress(event)" />
JavaScript
function handleImageProgress(event) {
if (event.lengthComputable) {
const percentComplete = Math.round((event.loaded / event.total) * 100);
console.log(`Image loading progress: ${percentComplete}%`);
}
}
Here, we use the img
element with its src
attribute set to the URL of an image file. We also add the onprogress
attribute to the img
element, which specifies the name of the function to handle the progress
event.
The handleImageProgress
function is defined in a <script>
tag and takes the event
object as its argument. We check if the total size of the resource is computable using the lengthComputable
property of the event object. If it is, we calculate the percentage of the resource that has been loaded and log it to the console.
As the image loads, the onprogress
event is fired periodically, and the handleImageProgress
function is called to update the progress of the operation.
Note that using inline event handlers like this is generally not recommended for maintainability reasons, but it can be useful for quick prototyping or experimentation.
Programmatic trigger
The ProgressEvent
and its related onprogress
event cannot be triggered programmatically using the progress()
method. The ProgressEvent
is a type of event object that is created by the browser and dispatched when certain events, such as file uploads or downloads, are in progress.
The progress()
method is used in a different context, specifically with the XMLHttpRequest
object, to update the progress of a file upload or download operation. When an XMLHttpRequest
object is used to transfer data, it emits a progress
event periodically to indicate the amount of data that has been transferred and the total size of the resource being transferred.
You can use the XMLHttpRequest
object to trigger the progress
event programmatically by setting the onprogress
property to a function that handles the event and then calling the send()
method of the XMLHttpRequest
object. However, note that the progress
event is typically triggered automatically by the browser as the data is being transferred, so there is usually no need to trigger it manually.
In short, the ProgressEvent
and its related onprogress
event cannot be triggered programmatically using the progress()
method, but the XMLHttpRequest
object can be used to trigger the progress
event programmatically when uploading or downloading files.
javascript