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ì?
//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"];
}
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ể đó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"];
}
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ỉ địnhPhươ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
//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"];
}
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àysetTimeout[function_name, time];
Hãy phá vỡ nó
0 là một phương pháp được sử dụng để tạo các sự kiện thời gian//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"]; }
- Nó chấp nhận hai tham số bắt buộc
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//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"]; }
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 địnhTạ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ênsetTimeout[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"];
}
0Sau 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];
2Cá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 runDò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 thiSau 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ôngHã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ứcDò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ệpMã 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ạyTuy 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 thiSau 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ạyBạ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];
8Sau đó, 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ọiCách sử dụng Phương thức setTimeout[function[] {
// function code goes here
}, time];
2 trong JavaScript
setTimeout[function[] {
// function code goes here
}, time];
Đ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ó íchCú pháp chung cho
setTimeout[function[] {
// function code goes here
}, time];
2 như sausetTimeout[function_name, time];
6Cá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ếnSau đó,
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"];
}
0Bâ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 ?
setTimeout[function[] {
// function code goes here
}, time];
setTimeout[function[] {
// function code goes here
}, time];
//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"];
}
5Tuy 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ạiTrong 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ênPhầ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
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