grayshine
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    ## <font class="h2">自動排程settimeout與 setInterval()</font> ###### tags: `javascript` <style> .h2 { background: linear-gradient(135deg,#fff,#537479) ; color: #537479; display:block; padding: 6px 5px; border-radius: 4px; } .h3 { background: linear-gradient(180deg,#fff 50%,#c9d5d4) ; color: #537479; display:block; padding: 6px 5px; border-bottom: 3px solid #537479; } .h4 { color: #537479; font-weight:bold; font-size:1.1em; } </style> :::info javascript是同步語言,中間有報錯程式就不會繼續跑 ::: 雖然setTimeout()這些timer方法不在ECMAScript的規格內,而==是屬於window的方法==,也就是BOM的一部分,不過多數瀏覽器與Node.js也都有實作這些timer方法,根據環境不同實作也多少有些差異,像是==瀏覽器的setTimeout/setInterval回傳的是Number,而在Node.js回傳的是Object。== <br> | 語法 |內容 | | ------------ | ------------------ | | setTimeout() | 只會執行一次就結束 | | setInterval()| 在間隔固定的時間不斷重複 | <br> ### <font class="h4">句法:</font> :::info ```javascript var timeoutID = scope.setTimeout(function[, 毫秒, param1, param2, ...]); var timeoutID = scope.setTimeout(function[, 豪秒]); var timeoutID = scope.setTimeout(code[, 豪秒]);//code可以為字串 ``` `[]`代表可以省略不寫 ::: <br> ### <font class="h3">setTimeout()</font> ### <font class="h4">➤範例一.</font> ```javascript let timeoutID =setTimeout(function(){console.log("Hello")},3000); ``` 或 ```javascript let timeoutID = window.setTimeout(( () => console.log("Hello!") ), 1000); ``` <br> ### <font class="h4">➤範例二.</font> 秒數後面,可以帶入參數 ![](https://i.imgur.com/Ijnw4pL.png) <br> ### <font class="h4">➤範例三.</font> ```javascript function showLog(){ console.log('倒數計時') } setTimeout(showLog,1000) ``` <br> ### <font class="h4">➤倒數計時範例:</font> ![](https://i.imgur.com/XsHy51h.png) ![](https://i.imgur.com/3ZSSAGU.gif) 但數字不會停下來 <br> ![](https://i.imgur.com/VZ1Z9mX.png) 可以使用if判斷停下來 <br><br><br><br> ### <font class="h4">➤`clearTimeout()`</font> 當我們呼叫setTimeout()與setInterval()的時候,它們會回傳一個獨立的timeID,這個ID就是當我們想取消setTimeout()與setInterval()的時候作為識別的數字。 <br><br><br> ```javascript let timeoutID = window.setTimeout(( () => console.log("Hello!") ), 1000); window.clearTimeout(timeoutID); ``` <br><br><br> ### 範例: ```javascript setTimeout( () => { console.log('倒數計時') },5000) ``` ![](https://i.imgur.com/Qo1lbMx.png) 會得到一個id數字,可以使用`clearTimeout()`來暫停setTimeout函式 <br><br> ```javascript setTimeout( () => { console.log('倒數計時') },5000) clearTimeout(2) ``` ### <font class="h3">setTimeout 遇到迴圈: IIFE範例</font> ```javascript for( var i = 0; i < 5; i++ ) { window.setTimeout(function () { console.log(i); }, 1000*i); } ``` 但是上面這段 code 執行的結果是, console.log() 會在「一秒鐘之後」同時印出「五次 5」。 這是為什麼呢? 由於 JavaScript 切分變數有效範圍 (scope) 的最小單位為 function,所以當 1000ms 過去, 變數 i 的值早已是 for 迴圈更新完畢的 i ,而不是迴圈內當下的那個 i <br> ```javascript for (var i = 0; i < 5; i++) { // 為了凸顯差異,我們將傳入後的參數改名為 x (function (x) { window.setTimeout(function () { console.log(x); }, 1000 * x); })(i); } ``` ![](https://i.imgur.com/7kozl28.png) 使用立即函式,就可以解決這個問題 <br> ```javascript for( let i = 0; i < 5; i++ ) { window.setTimeout(function () { console.log(i); }, 1000*i); } ``` 也可以使用let避開這個問題。 <br><br><br><br> ### <font class="h3">setInterval()</font> setInterval盡可能不要重複執行 ### <font class="h4">範例:</font> ```javascript let timeoutID = window.setInterval(( () => console.log("Hello!") ), 1000); ``` `setInterval() `一啟動之後就會在固定的間隔時間不斷執行 如果想要停下來,就需要用到 `clearInterval()` 來取消 `setInterval()`。 ```javascript let timeoutID = window.setInterval(( () => console.log("Hello!") ), 1000); window.clearInterval(timeoutID); ``` <br> ### <font class="h4">➤`clearInterval()`</font> `clearInterval()`可以暫停setInterval函式 ### 語法: ```javascript clearInterval(timeoutID) ``` <br><br><br><br> ### <font class="h3">紀錄停止編號(timeoutID)範例</font> 透過改寫javascript的方式,將setInterval的編號給紀錄下來 ```javascript window._setInterval = window.setInterval; window.setInterval = function(fun,time,...args){ window._setIntervalQueue = window._setIntervalQueue ||[]; let sid = window._setInterval(fun,time,...args); window._setIntervalQueue.push(sid); return sid; } window.clearIntervalAll = function(){ while(window._setIntervalQueue.length){ let sid = window._setIntervalQueue.shift(); window.clearInterval(sid) } } ``` ![](https://i.imgur.com/tVv6Y2O.png) ### 說明: ![](https://i.imgur.com/gjimbFV.png) ![](https://i.imgur.com/j2l878L.png) <br><br><br><br> ```javascript setInterval(function(){ console.log(Date.now()) },5000) _setIntervalQueue //就可以查看編號 clearIntervalAll() //setInterval就會停止了 ``` <br><br><br><br> ### <font class="h3">其他</font> setTimeout可以取用全域的變數 ```javascript const a = 'hello' function aaa() { setTimeout(function () { console.log(a) }, 1000) } aaa() //hello ``` --- [latentflip測試Stack Queue狀態](http://latentflip.com/loupe/?code=!!!PGJ1dHRvbj5DbGljayBtZSE8L2J1dHRvbj4%3D) https://ithelp.ithome.com.tw/articles/10007614 https://www.youtube.com/watch?v=d50tamoZa-M&ab_channel=DansonLin https://www.youtube.com/watch?v=60RkpSQYl_0&ab_channel=%E5%AE%89%E8%BF%AATV%23AndysTV-%E7%BE%8E%E9%A3%9F%2C%E7%A8%8B%E5%BC%8F%2C%E7%A7%91%E6%8A%80%2C%E8%BB%9F%E7%B6%B2 https://kuro.tw/posts/2019/02/23/%E8%AB%87%E8%AB%87-JavaScript-%E7%9A%84-setTimeout-%E8%88%87-setInterval/ https://ithelp.ithome.com.tw/articles/10227121 https://autoposter.pixnet.net/blog/post/116854913-js-%E4%B8%ADsettimeout%28%29%E7%9A%84%E7%94%A8%E6%B3%95%E8%A9%B3%E8%A7%A3 https://www.youtube.com/watch?v=I93LFPtBgL8&t=131s&ab_channel=%E5%BD%AD%E5%BD%AD%E7%9A%84%E8%AA%B2%E7%A8%8B

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully