Một Số Hàm Toàn cục Trong JavaScript | Global Functions

Trong JavaScript, có một số hàm toàn cầu (global functions) mà bạn có thể sử dụng mà không cần phải khai báo hay import từ các module. Dưới đây là một số hàm toàn cầu quan trọng:
  1. setTimeout(callback, delay, arg1, arg2, ...): Thực hiện một hàm callback sau một khoảng thời gian chờ đợi (miligiây).
    setTimeout(() => {
        console.log("Hello after 1000ms");
    }, 1000);
    
  2. setInterval(callback, delay, arg1, arg2, ...): Thực hiện một hàm callback lặp đi lặp lại sau mỗi khoảng thời gian chờ đợi.
    setInterval(() => {
        console.log("Hello every 2000ms");
    }, 2000);
    
  3. console.log(message, ...optionalParams): In ra một hoặc nhiều thông điệp trên console.
    console.log("Hello, World!");
    
  4. alert(message): Hiển thị một hộp thoại thông báo trên trình duyệt.
    alert("This is an alert!");
    
  5. prompt(message, default): Hiển thị một hộp thoại prompt để người dùng nhập dữ liệu.
    let userInput = prompt("Enter something:", "Default value");
    
  6. confirm(message): Hiển thị một hộp thoại xác nhận với các nút "OK" và "Cancel".
    let userConfirmed = confirm("Are you sure?");

Nhận xét

Mới hơn Cũ hơn