# requestIdleCallback

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .show-info {
        font-size: 14px;
        line-height: 2;
      }
      .btn-default {
        display: inline-block;
        padding: 6px 12px;
        margin-bottom: 0;
        font-size: 14px;
        font-weight: 400;
        line-height: 1.42857143;
        text-align: center;
        white-space: nowrap;
        vertical-align: middle;
        -ms-touch-action: manipulation;
        touch-action: manipulation;
        cursor: pointer;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
        background-image: none;
        border: 1px solid transparent;
        border-radius: 4px;
        color: #333;
        background-color: #fff;
        border-color: #ccc;
        margin-bottom: 10px;
      }
      .btn-default:hover {
        background-color: #ededed;
      }

      .c-f00 {
        color: #f00;
      }
    </style>
  </head>

  <body>
    <div class="m10">
      <h1>requestIdleCallback测试(timeout=100)</h1>
      <div class="blank-area">
        <p>rIC的复杂回调与复杂回调的优化</p>
        <div>
          <a id="btn1" class="btn-default" href="javascript:;">rIC复杂回调</a>
          <a id="btn2" class="btn-default" href="javascript:;"
            >rIC复杂回调(分段回调)</a
          >
        </div>
        <p id="info" class="show-info"></p>
      </div>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
    <script>
      var requestId = 0,
        info = $("#info"),
        html = "",
        secs = 0;

      if (!("requestIdleCallback" in window)) {
        info.html(
          "您的浏览器还不支持requestIdleCallback方法哦,建议使用chrome浏览器尝试~"
        );
      }

      var rAF = (function () {
        return (
          window.requestAnimationFrame ||
          window.webkitRequestAnimationFrame ||
          window.mozRequestAnimationFrame ||
          function (callback) {
            window.setTimeout(callback, 1000 / 60);
          }
        );
      })();

      function add(t, className) {
        info.append(
          '<span class = "' + (className || "") + '">' + t + "</span><br/>"
        );
      }

      function rIC(cb) {
        requestId = requestIdleCallback(cb);
      }

      function cb1(deadline) {
        var i = 0,
          len = 10000,
          secs = new Date().getTime();

        add("rIC回调中,执行了一个" + len + "长度的循环");
        for (i; i < len; i++) {
          if ((i * 10) % len == 0) {
            add("rIC 回调中,第" + i + "次循环", "c-f00");
          }
          console.log("i=" + i);
        }
        add("rIC消耗时间=" + (new Date().getTime() - secs), "c-f00");
      }

      function _cb1() {
        var i = 0,
          len = 5;

        info.html("");

        function _s() {
          if (i < len) {
            add("rAF 循环次数 i=" + i);
            rAF(_s);
            i++;
          }
        }

        rIC(cb1);

        rAF(_s);
      }
      $("#btn1").on("click", _cb1);

      function cb2(deadline) {
        var i = 0,
          len = 1000,
          secs = new Date().getTime();

        add("rIC回调中,执行了一个" + len + "长度的循环");
        function _s(deadline) {
          //表示该帧还有剩余时间
          for (i; i < len; i++) {
            if (deadline.timeRemaining() > 0) {
              console.log("i=" + i);

              if ((i * 10) % len == 0) {
                add("rIC 回调中,第" + i + "次循环", "c-f00");
              }
            } else {
              rIC(_s);
              break;
            }
          }
        }

        _s(deadline);
      }
      function _cb2() {
        var i = 0,
          len = 5;

        info.html("");

        function _s() {
          if (i < len) {
            add("rAF 循环次数 i=" + i);
            rAF(_s);
            i++;
          }
        }

        rIC(cb2);

        rAF(_s);
      }
      $("#btn2").on("click", _cb2);
    </script>
  </body>
</html>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
最后更新时间: 5/26/2021, 5:52:52 PM