SlideShare a Scribd company logo
1 of 47
Tommy Lin
林儀泰
FB/Email:tooto1985@gmail.com
 setTimeout
◦ 設定延遲執行
 setInterval
◦ 設定定時執行
 clearTimeout
◦ 清除延遲執行
 clearInterval
◦ 清除定時執行
 setTimeout為例
◦ 設定某一個function過n毫秒後執行
function run() {
console.log(“ok”);
}
setTimeout(run, 2000);
function run() {
console.log(“ok”);
}
setTimeout(run, 2000);
function run() {
console.log(“ok”);
}
setTimeout(run, 2000);
//過兩秒後
function run() {
console.log(“ok”);
}
setTimeout(run, 2000);
//過兩秒後
“ok”
 setTimeout為例
◦ 設定某一個function過n毫秒後執行
 setInterval為例
◦ 設定某一個function每n毫秒後執行
function run() {
console.log(“ok”);
}
setInterval(run, 2000);
function run() {
console.log(“ok”);
}
setInterval(run, 2000);
function run() {
console.log(“ok”);
}
setInterval(run, 2000);
//過兩秒後
function run() {
console.log(“ok”);
}
setInterval(run, 2000);
//過兩秒後
“ok”
function run() {
console.log(“ok”);
}
setInterval(run, 2000);
//過兩秒後
“ok”
//過兩秒後
function run() {
console.log(“ok”);
}
setInterval(run, 2000);
//過兩秒後
“ok”
//過兩秒後
“ok”
function run() {
console.log(“ok”);
}
setInterval(run, 2000);
//過兩秒後
“ok”
//過兩秒後
“ok”
//過兩秒後
function run() {
console.log(“ok”);
}
setInterval(run, 2000);
//過兩秒後
“ok”
//過兩秒後
“ok”
//過兩秒後
“ok”......
 setTimeout為例
◦ 設定某一個function過n毫秒後執行
 setInterval為例
◦ 設定某一個function每n毫秒後直行
 clearTimeout為例
◦ 清除某個setTimeout
function run() {
console.log(“ok”);
}
var id = setTimeout(run, 2000);
function run() {
console.log(“ok”);
}
var id = setTimeout(run, 2000);
function run() {
console.log(“ok”);
}
var id = setTimeout(run, 2000);
clearTimeout(id);
function run() {
console.log(“ok”);
}
var id = setTimeout(run, 2000);
clearTimeout(id);
 setTimeout為例
◦ 設定某一個function過n毫秒後執行
 setInterval為例
◦ 設定某一個function每n毫秒後直行
 clearTimeout為例
◦ 清除某個setTimeout
 clearInterval為例
◦ 清除某個setInterval
function run() {
console.log(“ok”);
}
var id = setInterval(run, 2000);
function run() {
console.log(“ok”);
}
var id = setInterval(run, 2000);
function run() {
console.log(“ok”);
}
var id = setInterval(run, 2000);
clearInterval(id);
function run() {
console.log(“ok”);
}
var id = setInterval(run, 2000);
clearInterval(id);
 無法改變function的this嗎?
document.body.addEventListener(“click”,function(){
console.log(this);
function run() {
console.log(this);
}
setTimeout(run, 2000);
});
document.body.addEventListener(“click”,function(){
console.log(this); //body
function run() {
console.log(this);
}
setTimeout(run, 2000);
});
document.body.addEventListener(“click”,function(){
console.log(this); //body
function run() {
console.log(this); //window !!!
}
setTimeout(run, 2000);
});
 無法改變function的this嗎?
◦ 使用bind解決!
document.body.addEventListener(“click”,function(){
console.log(this);
function run() {
console.log(this);
}
setTimeout(run.bind(this), 2000);
});
document.body.addEventListener(“click”,function(){
console.log(this);
function run() {
console.log(this); //body
}
setTimeout(run.bind(this), 2000);
});
 無法改變function的this嗎?
◦ 使用bind解決!
 沒有辦法傳入參數?
function run(a, b) {
console.log(a ,b);
}
setTimeout(run, 2000);
function run(a, b) {
console.log(a ,b); //undefined undefined
}
setTimeout(run, 2000);
 無法改變function的this嗎?
◦ 使用bind解決!
 沒有辦法傳入參數?
◦ 使用全域變數!
var a=10, b=20;
function run() {
console.log(a ,b);
}
setTimeout(run, 2000);
var a=10, b=20;
function run() {
console.log(a ,b); //10 20
}
setTimeout(run, 2000);
 無法改變function的this嗎?
◦ 使用bind解決!
 沒有辦法傳入參數?
◦ 使用全域變數!
◦ 使用bind傳入!
function run(a, b) {
console.log(a ,b);
}
setTimeout(run.bind(this, 10, 20), 2000);
function run(a, b) {
console.log(a ,b); //10 20
}
setTimeout(run.bind(this, 10, 20), 2000);
 無法改變function的this嗎?
◦ 使用bind解決!
 沒有辦法傳入參數?
◦ 使用全域變數!
◦ 使用bind傳入!
◦ 使用setTimeout第三個以後的參數!
function run(a, b) {
console.log(a ,b);
}
setTimeout(run ,2000 ,10 ,20);
function run(a, b) {
console.log(a ,b); //10 20
}
setTimeout(run ,2000 ,10 ,20);
 setTimeout/setInterval會造成程式非同步執行
 執行編號非每次都是從0開始(不同瀏覽器起始值都
不一樣)
 https://developer.mozilla.org/en-
US/docs/Web/API/WindowTimers/setTimeout

More Related Content

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

簡單介紹JavaScript計時器