Chức năng chạy Python cứ sau n mili giây

Bạn có thể cần một số dòng mã nhất định để thực thi tại một thời điểm trong tương lai, khi bạn chỉ định rõ ràng, thay vì tất cả các mã thực thi đồng bộ

Một cái gì đó như thế là có thể với JavaScript

Trong bài viết này, bạn sẽ tìm hiểu về phương pháp

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0 – nó là gì và bạn có thể sử dụng nó như thế nào trong các chương trình của mình

Đây là những gì chúng tôi sẽ trình bày trong hướng dẫn nhanh này

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0 trong JavaScript là gì?

Vai trò của

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0, một phương thức không đồng bộ của đối tượng cửa sổ, là đặt bộ đếm thời gian sẽ thực thi một hành động. Đồng hồ bấm giờ đó cho biết một thời điểm cụ thể khi sẽ có một yếu tố kích hoạt cho hành động cụ thể đó

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0 là một phần của đối tượng cửa sổ nên nó cũng có thể được viết là
setTimeout[function[] {
    // function code goes here
}, time];
8. Điều đó nói rằng, tiền tố
setTimeout[function[] {
    // function code goes here
}, time];
9 được ngụ ý và do đó thường được bỏ qua và không được chỉ định

Phương pháp
//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0 - Tổng quan về cú pháp

Cú pháp chung của phương thức

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0 trông như thế này

setTimeout[function_name, time];

Hãy phá vỡ nó

  • //this code is executed first
    
    console.log["Where can I learn to code for free and get a developer job?"];
    
    // this line indicates that the function definition will be executed once 3ms have passed
    
    setTimeout[codingCourse, 3000];
    
    
    //function definition
    
    function codingCourse[] {
      console.log["freeCodeCamp"];
    }
    
    0 là một phương pháp được sử dụng để tạo các sự kiện thời gian
  • Nó chấp nhận hai tham số bắt buộc
  • //this code is executed first
    
    console.log["Where can I learn to code for free and get a developer job?"];
    
    // this line indicates that the function definition will be executed once 3ms have passed
    
    setTimeout[codingCourse, 3000];
    
    
    //function definition
    
    function codingCourse[] {
      console.log["freeCodeCamp"];
    }
    
    3 là tham số bắt buộc đầu tiên. Đó là tên của hàm gọi lại chứa mã bạn muốn thực thi. Tên của hàm hoạt động như một tham chiếu và con trỏ tới định nghĩa hàm chứa khối mã thực
  • //this code is executed first
    
    console.log["Where can I learn to code for free and get a developer job?"];
    
    // this line indicates that the function definition will be executed once 3ms have passed
    
    setTimeout[codingCourse, 3000];
    
    
    //function definition
    
    function codingCourse[] {
      console.log["freeCodeCamp"];
    }
    
    4 là tham số bắt buộc thứ hai và được xác định bằng mili giây [để tham khảo,
    //this code is executed first
    
    console.log["Where can I learn to code for free and get a developer job?"];
    
    // this line indicates that the function definition will be executed once 3ms have passed
    
    setTimeout[codingCourse, 3000];
    
    
    //function definition
    
    function codingCourse[] {
      console.log["freeCodeCamp"];
    }
    
    5]. Nó đại diện cho lượng thời gian đã chỉ định mà chương trình phải đợi để chức năng được thực thi

Nhìn chung, điều này có nghĩa là

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0 sẽ thực thi mã có trong một hàm nhất định một lần và chỉ sau một khoảng thời gian xác định

Tại thời điểm này, điều đáng nói là thay vì chuyển tên hàm, bạn có thể chuyển một hàm ẩn danh tới

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0

Điều này thuận tiện khi chức năng không chứa nhiều dòng mã

Hàm ẩn danh có nghĩa là bạn nhúng mã trực tiếp làm tham số đầu tiên trong

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0 và không tham chiếu tên hàm như bạn đã thấy ở trên

setTimeout[function[] {
    // function code goes here
}, time];

Một điều khác cần lưu ý là

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0 trả về một
console.log["Good Morning!"];

setTimeout[function[] {
  console.log["Good Night!"];
}, 1000];

console.log["Good Afternoon!"];
0 – một số nguyên dương xác định bộ đếm thời gian được tạo bởi
//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0

Sau này bạn sẽ thấy giá trị của

console.log["Good Morning!"];

setTimeout[function[] {
  console.log["Good Night!"];
}, 1000];

console.log["Good Afternoon!"];
0 được sử dụng như thế nào với phương thức
setTimeout[function[] {
    // function code goes here
}, time];
2

Cách đợi N giây trong JavaScript

Hãy xem một ví dụ về cách áp dụng

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}

Mã JavaScript được thực thi từ trên xuống dưới

Dòng mã đầu tiên,

console.log["Good Morning!"];

setTimeout[function[] {
  console.log["Good Night!"];
}, 1000];

console.log["Good Afternoon!"];
5, được thực thi ngay khi tôi nhấn run

Dòng mã thứ hai chỉ ra rằng cần phải có độ trễ theo lịch trình là 3000 mili giây [hoặc 3 giây] trước khi mã trong hàm

console.log["Good Morning!"];

setTimeout[function[] {
  console.log["Good Night!"];
}, 1000];

console.log["Good Afternoon!"];
6 được thực thi

Sau khi 3000 mili giây trôi qua, bạn sẽ thấy mã bên trong hàm [

console.log["Good Morning!"];

setTimeout[function[] {
  console.log["Good Night!"];
}, 1000];

console.log["Good Afternoon!"];
7] thực thi thành công

Hãy xem một ví dụ khác

console.log["Good Morning!"];

setTimeout[function[] {
  console.log["Good Night!"];
}, 1000];

console.log["Good Afternoon!"];

Trong ví dụ trên, dòng mã đầu tiên,

console.log["Good Morning!"];

setTimeout[function[] {
  console.log["Good Night!"];
}, 1000];

console.log["Good Afternoon!"];
8, thực thi ngay lập tức

Dòng

console.log["Good Morning!"];

setTimeout[function[] {
  console.log["Good Night!"];
}, 1000];

console.log["Good Afternoon!"];
9 cũng vậy, mặc dù nó là dòng mã cuối cùng trong tệp

Mã trong

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0 chỉ ra rằng cần có độ trễ một giây trước khi chạy

Tuy nhiên, trong thời gian đó, việc thực thi phần còn lại của mã trong tệp không bị tạm dừng

Thay vào đó, dòng đó tạm thời bị bỏ qua và dòng

console.log["Good Morning!"];

setTimeout[function[] {
  console.log["Good Night!"];
}, 1000];

console.log["Good Afternoon!"];
9 được thực thi

Sau khi một giây đó trôi qua, mã trong

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0 sẽ chạy

Bạn cũng có thể chuyển các tham số tùy chọn khác cho ________ 40

Trong ví dụ bên dưới, hàm _______ 184 chấp nhận hai đối số, ________ 185 và ________ 186

setTimeout[function[] {
    // function code goes here
}, time];
8

Sau đó, chúng được chuyển đến phương thức

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0 và sẽ có độ trễ là 3 giây sau khi hàm được gọi

Cách sử dụng Phương thức
setTimeout[function[] {
    // function code goes here
}, time];
2 trong JavaScript

Điều gì xảy ra nếu bạn muốn hủy bỏ sự kiện thời gian mà bạn đã tạo?

Bạn có thể ngăn mã trong

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0 chạy bằng cách sử dụng phương pháp
setTimeout[function[] {
    // function code goes here
}, time];
2. Đây là nơi mà
console.log["Good Morning!"];

setTimeout[function[] {
  console.log["Good Night!"];
}, 1000];

console.log["Good Afternoon!"];
0 được đề cập trước đó có ích

Cú pháp chung cho

setTimeout[function[] {
    // function code goes here
}, time];
2 như sau

setTimeout[function_name, time];
6

Cách thức hoạt động của điều này là bạn phải lưu

console.log["Good Morning!"];

setTimeout[function[] {
  console.log["Good Night!"];
}, 1000];

console.log["Good Afternoon!"];
0, được trả về sau mỗi lần gọi
//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0, vào một biến

Sau đó,

console.log["Good Morning!"];

setTimeout[function[] {
  console.log["Good Night!"];
}, 1000];

console.log["Good Afternoon!"];
0 được sử dụng làm tham số cho
setTimeout[function[] {
    // function code goes here
}, time];
2, như bên dưới

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0

Bây giờ, mã trong

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0 sẽ không thực thi

Đâu là sự khác biệt giữa
setTimeout[function[] {
    // function code goes here
}, time];
3 và
setTimeout[function[] {
    // function code goes here
}, time];
4 ?

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0 và
//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
01 có cú pháp rất giống nhau

Đây là cú pháp cho

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
01

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
5

Tuy nhiên, không nên sử dụng chúng thay thế cho nhau vì chúng hoạt động theo những cách khác nhau

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0 kích hoạt một hành động một lần, trong khi
//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
01 kích hoạt một hành động lặp đi lặp lại

Trong ví dụ bên dưới, hàm

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
05 được gọi ba giây một lần

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
8

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
01 là một lựa chọn tốt khi bạn muốn lặp lại điều gì đó thường xuyên

Phần kết luận

Và bạn có nó rồi đấy. Bây giờ bạn đã biết kiến ​​thức cơ bản về cách thức hoạt động của

//this code is executed first

console.log["Where can I learn to code for free and get a developer job?"];

// this line indicates that the function definition will be executed once 3ms have passed

setTimeout[codingCourse, 3000];


//function definition

function codingCourse[] {
  console.log["freeCodeCamp"];
}
0 và cách tạo các sự kiện tính thời gian trong JavaScript

Để tìm hiểu thêm về JavaScript, hãy xem Chứng nhận cấu trúc dữ liệu và thuật toán JavaScript của freeCodeCamp

Đó là một chương trình giảng dạy miễn phí, được cân nhắc kỹ lưỡng và có cấu trúc, nơi bạn sẽ học một cách tương tác. Cuối cùng, bạn cũng sẽ xây dựng 5 dự án để nhận chứng chỉ và củng cố kiến ​​thức của mình bằng cách áp dụng các kỹ năng mới của bạn vào thực tế

Cảm ơn vì đã đọc

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

Dionysia Lemonaki

Học một cái gì đó mới mỗi ngày và viết về nó

Nếu bài viết này hữu ích, hãy tweet nó

Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu

Chủ Đề