随机时间和随机倒计时用法技巧分享
<p></p><div><br></div><div></div><div><br></div><div><div class="DocReader-module_header_xAOtU" static="" image="" smiley="" default="" titter.gif"="" smilieid="9" border="0" alt="" style="box-sizing: border-box; max-width: 100%; padding-right: 50px; padding-left: 50px; margin-right: var(--viewer-center-align-right); margin-left: auto; color: rgb(38, 38, 38); font-family: "Chinese Quote", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "<img src=";"><div class="blockcode" style="overflow: hidden; padding-right: 0px; padding-bottom: 5px; padding-left: 10px; background-image: linear-gradient(90deg, rgb(237, 237, 237), rgb(237, 237, 237)); background-size: 43px; color: rgb(102, 102, 102); zoom: 1;"><blockquote>function 随机倒计时(minSeconds, maxSeconds) {<br>var randomInterval = Math.floor(Math.random() * (maxSeconds - minSeconds + 1) + minSeconds);<br>printl("倒计时开始: " + randomInterval + " 秒");<br><br><br>var intervalId = setInterval(function() {<br>printl(randomInterval + " 秒剩余");<br><br>if (randomInterval <= 0) {<br>clearInterval(intervalId);<br>printl("倒计时结束");<br>}<br><br>randomInterval--;<br>}, 1000);<br>}<br><br><br>// 调用函数,设置随机倒计时在5到10秒之间<br>随机倒计时(5, 10);<br><br><br><br></blockquote></div><br></div><div class="DocReader-module_header_xAOtU" static="" image="" smiley="" default="" titter.gif"="" smilieid="9" border="0" alt="" style="box-sizing: border-box; max-width: 100%; padding-right: 50px; padding-left: 50px; margin-right: var(--viewer-center-align-right); margin-left: auto; color: rgb(38, 38, 38); font-family: "Chinese Quote", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "<img src=";"><span style="font-family: "Helvetica Neue", Consolas; text-wrap: nowrap; font-variant-ligatures: none; color: var(--yq-text-primary); font-size: 15px;"><br></span></div><div class="DocReader-module_header_xAOtU" static="" image="" smiley="" default="" titter.gif"="" smilieid="9" border="0" alt="" style="box-sizing: border-box; max-width: 100%; padding-right: 50px; padding-left: 50px; margin-right: var(--viewer-center-align-right); margin-left: auto; color: rgb(38, 38, 38); font-family: "Chinese Quote", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "<img src=";"><span style="font-family: "Helvetica Neue", Consolas; text-wrap: nowrap; font-variant-ligatures: none; color: var(--yq-text-primary); font-size: 15px;">1</span><br></div><div static="" image="" smiley="" default="" titter.gif"="" smilieid="9" border="0" alt="" style="box-sizing: border-box; color: rgb(38, 38, 38); font-family: "Chinese Quote", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "<img src=";"><article id="content" class="article-content" tabindex="0" style="box-sizing: border-box; outline-style: none;"><div class="ne-doc-major-viewer" style="box-sizing: border-box;"><div class="yuque-doc-content" data-df="lake" style="box-sizing: border-box; position: relative;"><div style="box-sizing: border-box;"><div class="ne-viewer lakex-yuque-theme-light ne-typography-classic ne-paragraph-spacing-relax ne-viewer-layout-mode-adapt" data-viewer-mode="normal" id="u9c76" static="" image="" smiley="" default="" titter.gif"="" smilieid="9" border="0" alt="" style="box-sizing: border-box; position: relative; line-height: 1.74; color: var(--yq-text-primary); font-size: 15px; --lakex-editor-background: #FFFFFF; --lakex-editor-background-primary: #fff; --lakex-editor-background-primary-hover: #EFF0F0; --lakex-editor-background-primary-hover-light: #E7E9E8; --lakex-editor-background-secondary: #FAFAFA; --lakex-editor-background-tertiary: #F4F5F5; --lakex-editor-overlay-background-tertiary: #F4F5F5; --lakex-editor-background-foreground: #fff; --lakex-editor-border-primary: #E7E9E8; --lakex-editor-border-secondary: #D8DAD9; --lakex-editor-border-light: rgba(0, 0, 0, 0.04); --lakex-editor-code-background: #0000000f; --lakex-editor-table-scrollbar-thumb: #00000033; --lakex-editor-card-border-hover: #81BBF8; --lakex-editor-card-border-selected: #2F8EF4; --lakex-editor-table-bar-active: #2F8EF4CC; --lakex-editor-text-link: #117CEE; --lakex-editor-text-link-hover: #5FA8F6; --lakex-editor-column-background-highlight: #D9EAFC7F; --lakex-editor-card-background-hover: #5FA8F633; --lakex-editor-background-focus: #D9EAFC; --lakex-editor-text-color: #262626; --lakex-editor-text-body: #585A5A; --lakex-editor-text-caption: #8A8F8D; --lakex-editor-icon-primary: #000; --lakex-editor-icon-secondary: #585A5A; --lakex-editor-text-disable: #BEC0BF; --lakex-editor-table-bar-danger: #E4495B5A; --lakex-editor-text-danger-light: #EA7583; --lakex-editor-text-danger: #DF2A3F; --lakex-editor-color-theme: #00B96B; --lakex-editor-color-theme-light: #0BD07D; --lakex-editor-cursor: #009456; --lakex-editor-selection: rgba(27, 162, 227, 0.2); --lakex-editor-color-black: #000; --lakex-editor-color-black-f05: rgba(0,0,0,0.05); --lakex-editor-color-black-f08: rgba(0,0,0,0.08); --lakex-editor-color-black-f12: rgba(0,0,0,0.12); --lakex-editor-color-black-f30: rgba(0,0,0,0.30); --lakex-editor-color-black-f65: rgba(0,0,0,0.65); --lakex-editor-color-blue1: #D9EAFC; --lakex-editor-color-blue8: #074A92; --lakex-editor-color-blue9: #00346B; --lakex-editor-color-peaGreen1: #E8F7CF; --lakex-editor-color-peaGreen8: #406105; --lakex-editor-color-peaGreen9: #2A4200; --lakex-editor-color-red1: #FBE4E7; --lakex-editor-color-red9: #70000D; --lakex-editor-color-white: #fff; --lakex-editor-color-white-f60: rgba(255,255,255,0.6); --lakex-editor-color-white-f90: rgba(255,255,255,0.9); --lakex-editor-color-yellow5: #F3BB2F; --lakex-label-color0-bg: #F8CED3; --lakex-label-color0-border: #F1A2AB; --lakex-label-color0-text: #70000D; --lakex-label-color1-bg: #F6E1AC; --lakex-label-color1-border: #F5D480; --lakex-label-color1-text: #664900; --lakex-label-color2-bg: #DBF1B7; --lakex-label-color2-border: #C1E77E; --lakex-label-color2-text: #2A4200; --lakex-label-color3-bg: #EFF0F0; --lakex-label-color3-border: #EFF0F0; --lakex-label-color3-text: #262626; --lakex-label-color4-bg: #C0DDFC; --lakex-label-color4-border: #81BBF8; --lakex-label-color4-text: #00346B; --lakex-label-color5-bg: #C0CAFC; --lakex-label-color5-border: #96A7FD; --lakex-label-color5-text: #101E60; --lakex-calendar-blue-line: #117CEE; --lakex-calendar-blue-border: #81BBF8; --lakex-calendar-blue-background: #D9EAFC; --lakex-calendar-magenta-line: #D22D8D; --lakex-calendar-magenta-border: #F297CC; --lakex-calendar-magenta-background: #FBDFEF; --lakex-calendar-green-line: #74B602; --lakex-calendar-green-border: #C1E77E; --lakex-calendar-green-background: #E8F7CF; --lakex-calendar-red-line: #DF2A3F; --lakex-calendar-red-border: #F1A2AB; --lakex-calendar-red-background: #FBE4E7; --lakex-calendar-purple-line: #601BDE; --lakex-calendar-purple-border: #BA9BF2; --lakex-calendar-purple-background: #E6DCF9; --lakex-calendar-yellow-line: #ECAA04; --lakex-calendar-yellow-border: #F5D480; --lakex-calendar-yellow-background: #F9EFCD; --lakex-alert-tips-bar-border: #BEC0BF; --lakex-alert-tips-bar-bg: #D8DAD9; --lakex-alert-tips-background: #EFF0F0; --lakex-alert-tips-border: #E7E9E8; --lakex-alert-info-bar-border: #C0DDFC; --lakex-alert-info-bar-bg: var(--lakex-alert-info-bar-border); --lakex-alert-info-background: rgba(192,221,252,0.5); --lakex-alert-info-border: #81BBF8; --lakex-alert-color1-bar-border: #B5EFF2; --lakex-alert-color1-bar-bg: var(--lakex-alert-color1-bar-border); --lakex-alert-color1-background: rgba(181,239,242,0.5); --lakex-alert-color1-border: #81DFE4; --lakex-alert-color2-bar-border: #C7F0DF; --lakex-alert-color2-bar-bg: var(--lakex-alert-color2-bar-border); --lakex-alert-color2-background: rgba(199,240,223,0.5); --lakex-alert-color2-border: #82EDC0; --lakex-alert-success-bar-border: #DBF1B7; --lakex-alert-success-bar-bg: var(--lakex-alert-success-bar-border); --lakex-alert-success-background: rgba(219,241,183,0.5); --lakex-alert-success-border: #C1E77E; --lakex-alert-warning-bar-border: #F6E1AC; --lakex-alert-warning-bar-bg: var(--lakex-alert-warning-bar-border); --lakex-alert-warning-background: rgba(246,225,172,0.5); --lakex-alert-warning-border: #F5D480; --lakex-alert-color3-bar-border: #F8D6B9; --lakex-alert-color3-bar-bg: var(--lakex-alert-color3-bar-border); --lakex-alert-color3-background: rgba(248,214,185,0.5); --lakex-alert-color3-border: #F8B881; --lakex-alert-danger-bar-border: #F8CED3; --lakex-alert-danger-bar-bg: var(--lakex-alert-danger-bar-border); --lakex-alert-danger-background: rgba(248,206,211,0.5); --lakex-alert-danger-border: #F1A2AB; --lakex-alert-color4-bar-border: #F7C4E2; --lakex-alert-color4-bar-bg: var(--lakex-alert-color4-bar-border); --lakex-alert-color4-background: rgba(247,196,226,0.5); --lakex-alert-color4-border: #F297CC; --lakex-alert-color5-bar-border: #D9C9F8; --lakex-alert-color5-bar-bg: var(--lakex-alert-color5-bar-border); --lakex-alert-color5-background: rgba(217,201,248,0.5); --lakex-alert-color5-border: #BA9BF2; --lakex-editor-bgColor-default: #FBDE28; --lakex-editor-color-default: #DF2A3F; --lakex-editor-table-cellBgColor-default: #F4F5F5; font-family: "<img src=";"><div class="ne-viewer-body" style="box-sizing: border-box; position: relative; z-index: 1; outline: none; white-space-collapse: break-spaces; word-break: break-word; font-variant-ligatures: none; --link-color: var(--yq-text-link);"><ne-oli index-type="0" style="box-sizing: border-box; display: flex; justify-content: flex-start; align-items: baseline; padding-left: 2em; margin-bottom: 3.915px; margin-right: var(--viewer-center-align-right);"><ne-oli-c class="ne-oli-content" id="ud0e13c78" data-lake-id="ud0e13c78" style="box-sizing: border-box; flex: 1 1 0%; min-width: 1px;"><ne-text id="udc6251d1" style="box-sizing: border-box;">随机倒计时函数 随机倒计时(minSeconds, maxSeconds)</ne-text><ne-text id="u8e3954ec" style="box-sizing: border-box;">功能说明: 这个函数用于生成一个随机的倒计时,倒计时的时间范围在 minSeconds 和 maxSeconds 之间。</ne-text><span class="ne-viewer-b-filler" ne-filler="block" style="box-sizing: border-box; display: inline; min-width: 1px; vertical-align: baseline; font-size: 1em; height: 1em; user-select: none;"><br style="box-sizing: border-box;"></span></ne-oli-c></ne-oli><ne-p id="ueb251960" data-lake-id="ueb251960" style="box-sizing: border-box; display: block; min-height: 24px; margin-bottom: 7.83px; margin-top: 7.83px; margin-right: var(--viewer-center-align-right);"><ne-text id="ub5af5ae2" style="box-sizing: border-box;">代码解析:</ne-text><span class="ne-viewer-b-filler" ne-filler="block" style="box-sizing: border-box; display: inline; min-width: 1px; vertical-align: baseline; font-size: 1em; height: 1em; user-select: none;"><br style="box-sizing: border-box;"></span></ne-p><ne-p id="u73489013" data-lake-id="u73489013" style="box-sizing: border-box; display: block; min-height: 24px; margin-bottom: 7.83px; margin-right: var(--viewer-center-align-right);"><ne-text id="u77228429" style="box-sizing: border-box;">var randomInterval = Math.floor(Math.random() * (maxSeconds - minSeconds + 1) + minSeconds);
</ne-text><ne-text id="u91f75a12" style="box-sizing: border-box;">这行代码生成一个介于 minSeconds 和 maxSeconds 之间的随机整数,作为倒计时的总秒数。</ne-text><span class="ne-viewer-b-filler" ne-filler="block" style="box-sizing: border-box; display: inline; min-width: 1px; vertical-align: baseline; font-size: 1em; height: 1em; user-select: none;"><br style="box-sizing: border-box;"></span></ne-p><ne-p id="u473df181" data-lake-id="u473df181" style="box-sizing: border-box; display: block; min-height: 24px; margin-bottom: 7.83px; margin-right: var(--viewer-center-align-right);"><ne-text id="u7ea50af0" style="box-sizing: border-box;">printl("倒计时开始: " + randomInterval + " 秒");
</ne-text><ne-text id="ue3a1f236" style="box-sizing: border-box;">打印倒计时的总秒数,表示倒计时开始。http://bbs.aiwork24.com/thread-133-1-1.html</ne-text></ne-p><ne-p id="ud167374d" data-lake-id="ud167374d" style="box-sizing: border-box; display: block; min-height: 24px; margin-bottom: 7.83px; margin-right: var(--viewer-center-align-right);"><ne-text id="u67d87ed5" style="box-sizing: border-box;">var intervalId = setInterval(function() {...}, 1000);
</ne-text><ne-text id="u783b0cc0" style="box-sizing: border-box;">使用 setInterval 函数每秒执行一次回调函数,实现倒计时的功能。</ne-text><span class="ne-viewer-b-filler" ne-filler="block" style="box-sizing: border-box; display: inline; min-width: 1px; vertical-align: baseline; font-size: 1em; height: 1em; user-select: none;"><br style="box-sizing: border-box;"></span></ne-p><ne-p id="u3c887064" data-lake-id="u3c887064" style="box-sizing: border-box; display: block; min-height: 24px; margin-bottom: 7.83px; margin-right: var(--viewer-center-align-right);"><ne-text id="u1948a5ce" style="box-sizing: border-box;">printl(randomInterval + " 秒剩余");
</ne-text><ne-text id="ua873292f" style="box-sizing: border-box;">每秒打印剩余的秒数。</ne-text><span class="ne-viewer-b-filler" ne-filler="block" style="box-sizing: border-box; display: inline; min-width: 1px; vertical-align: baseline; font-size: 1em; height: 1em; user-select: none;"><br style="box-sizing: border-box;"></span></ne-p><ne-p id="u5b9dfc85" data-lake-id="u5b9dfc85" style="box-sizing: border-box; display: block; min-height: 24px; margin-bottom: 7.83px; margin-right: var(--viewer-center-align-right);"><ne-text id="u0bf3543b" style="box-sizing: border-box;">if (randomInterval <= 0) { clearInterval(intervalId); printl("倒计时结束"); }
</ne-text><ne-text id="u9aa99795" style="box-sizing: border-box;">当倒计时结束时,清除 setInterval 并打印“倒计时结束”。</ne-text><span class="ne-viewer-b-filler" ne-filler="block" style="box-sizing: border-box; display: inline; min-width: 1px; vertical-align: baseline; font-size: 1em; height: 1em; user-select: none;"><br style="box-sizing: border-box;"></span></ne-p><ne-p id="uf78bb2a7" data-lake-id="uf78bb2a7" style="box-sizing: border-box; display: block; min-height: 24px; margin-bottom: 7.83px; margin-right: var(--viewer-center-align-right);"><ne-text id="u9bbd16a3" style="box-sizing: border-box;">randomInterval--;
</ne-text><ne-text id="u522b9e99" style="box-sizing: border-box;">每秒减少剩余的秒数。</ne-text><span class="ne-viewer-b-filler" ne-filler="block" style="box-sizing: border-box; display: inline; min-width: 1px; vertical-align: baseline; font-size: 1em; height: 1em; user-select: none;"><br style="box-sizing: border-box;"></span></ne-p><ne-p id="u19a9d9b9" data-lake-id="u19a9d9b9" style="box-sizing: border-box; display: block; min-height: 24px; margin-bottom: 7.83px; margin-right: var(--viewer-center-align-right);"><ne-text id="ue1800f00" style="box-sizing: border-box;">示例调用: 随机倒计时(5, 10);
</ne-text><ne-text id="uf50a9b96" style="box-sizing: border-box;">这个调用会生成一个介于5到10秒之间的随机倒计时。</ne-text><span class="ne-viewer-b-filler" ne-filler="block" style="box-sizing: border-box; display: inline; min-width: 1px; vertical-align: baseline; font-size: 1em; height: 1em; user-select: none;"><br style="box-sizing: border-box;"></span></ne-p><ne-p id="ua12be611" data-lake-id="ua12be611" style="box-sizing: border-box; display: block; min-height: 24px; margin-bottom: 7.83px; margin-right: var(--viewer-center-align-right);"><span class="ne-viewer-b-filler" ne-filler="block" style="box-sizing: border-box; display: inline; min-width: 1px; vertical-align: baseline; font-size: 1em; height: 1em; user-select: none;"></span></ne-p><ne-hole id="ue3938da5" data-lake-id="ue3938da5" style="box-sizing: border-box; max-width: calc(100% - var(--viewer-center-align-value)); width: 1209px; display: flex; margin-bottom: 7.83px; margin-right: var(--viewer-center-align-right);"><ne-card data-card-name="codeblock" data-card-type="block" id="ONOVh" data-event-boundary="card" class="" style="box-sizing: border-box; position: relative; z-index: 1; line-height: 1em; font-size: inherit; display: block; width: calc(100% - 2px); max-width: 100%; border-radius: 4px; border: 1px solid var(--lakex-editor-border-primary); margin-top: 2px; margin-bottom: 2px; height: auto !important;"><span class="ne-v-codeblock-hold" style="box-sizing: border-box; position: absolute; z-index: -1; width: 0px; height: 0px; overflow: hidden;"></span><div class="ne-card-container" data-width="1205" data-height="484.828125" style="box-sizing: border-box; position: relative; line-height: 1.74; overflow: hidden; border-radius: 4px; width: 1205px; background-color: var(--yq-bg-primary);"><div data-codeblock-mode="plain" class="ne-code ne-codeblock ne-code-viewer code-fz14 ne-codeblock-height-limit hide-toolbar" theme="Github Light" style="box-sizing: border-box; padding-top: 5px; position: relative; overflow: visible; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; width: 1205px; height: auto !important;"><div class="ne-codeblock-inner" style="box-sizing: border-box; padding: initial; position: relative; z-index: 2; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><div class="cm-editorͼ1 ͼ2 ͼ4 ͼg ͼl" style="box-sizing: border-box; flex-direction: column; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; position: relative !important; display: flex !important;"><div class="cm-announced" aria-live="polite" style="box-sizing: border-box; position: fixed; top: -10000px;"></div><div tabindex="-1" class="cm-scroller" style="box-sizing: border-box; line-height: 1.45; height: 479.828px; overflow-x: auto; position: relative; z-index: 0; overflow-anchor: none; font-size: 14px; color: rgb(89, 89, 89); max-height: 12600px; padding-bottom: 5px; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace !important; display: flex !important; align-items: flex-start !important;"><div class="cm-gutters" aria-hidden="true" style="box-sizing: border-box; flex-shrink: 0; display: flex; height: 474.828px; inset-inline-start: 0px; z-index: 200; color: rgb(108, 108, 108); border: none; min-height: 474.828px; position: sticky;"><div class="cm-gutter cm-lineNumbers" style="box-sizing: border-box; flex-direction: column; flex-shrink: 0; min-height: 100%; overflow: hidden; display: flex !important;"><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 0px; visibility: hidden; pointer-events: none;"></div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px; margin-top: 4px;">1</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">2</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">3</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">4</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">5</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">6</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">7</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">8</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">9</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">10</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">11</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">12</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">13</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">14</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">15</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">16</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">17</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">18</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">19</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">20</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">21</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">22</div><div class="cm-gutterElement" style="box-sizing: border-box; padding-right: 3px; padding-left: 15px; min-width: 20px; text-align: right; white-space: nowrap; height: 20.2969px;">23</div></div><div class="cm-gutter cm-foldGutter" style="box-sizing: border-box; flex-direction: column; flex-shrink: 0; min-height: 100%; overflow: hidden; display: flex !important;"><div class="cm-gutterElement" style="box-sizing: border-box; height: 0px; visibility: hidden; pointer-events: none;"><div class="gutter-fold-close" style="box-sizing: border-box; cursor: pointer; width: 16px; height: 19px; background-position: center center; background-image: url("data:image/svg+xml,%3Csvg width='16' height='16' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'%3E%3Cdefs%3E%3Cpath id='a' d='M0 0h16v16H0z'/%3E%3C/defs%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cmask id='b' fill='%23fff'%3E%3Cuse xlink:href='%23a'/%3E%3C/mask%3E%3Cpath d='m11.319 7.433-2.882 2.886a.618.618 0 0 1-.874 0L4.68 7.433a.62.62 0 0 1 .438-1.058h5.762a.62.62 0 0 1 .438 1.058Z' fill='%238C8C8C' fill-rule='nonzero' mask='url(%23b)'/%3E%3C/g%3E%3C/svg%3E"); transform-origin: center center; transform: rotateZ(-90deg);"></div></div></div></div><div spellcheck="false" autocorrect="off" autocapitalize="off" writingsuggestions="false" translate="no" contenteditable="false" class="cm-content" role="textbox" aria-multiline="true" aria-readonly="true" style="overflow-wrap: normal; box-sizing: border-box; flex-grow: 2; flex-shrink: 0; white-space: pre; min-height: 100%; padding-top: 4px; padding-bottom: 4px; outline: none; tab-size: 2; caret-color: transparent !important;"><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;">function 随机时间() {</div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;"> // 生成随机小时、分钟和秒数</div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;"> var hours = Math.floor(Math.random() * 24);</div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;"> var minutes = Math.floor(Math.random() * 60);</div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;"> var seconds = Math.floor(Math.random() * 60);</div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;"><br style="box-sizing: border-box;"></div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;"><br style="box-sizing: border-box;"></div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;"> // 格式化时间为两位数</div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;"> hours = hours < 10 ? '0' + hours : hours;</div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;"> minutes = minutes < 10 ? '0' + minutes : minutes;</div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;"> seconds = seconds < 10 ? '0' + seconds : seconds;</div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;"><br style="box-sizing: border-box;"></div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;"><br style="box-sizing: border-box;"></div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;"> // 返回格式化后的时间字符串</div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;"> return hours + ':' + minutes + ':' + seconds;</div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;">}</div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;"><br style="box-sizing: border-box;"></div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;"><br style="box-sizing: border-box;"></div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;">// 示例调用</div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;">printl(随机时间());</div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;"><br style="box-sizing: border-box;"></div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;"><br style="box-sizing: border-box;"></div><div class="cm-line" style="box-sizing: border-box; padding-right: 2px; padding-left: 6px; color: rgb(38, 44, 49); caret-color: transparent !important;"><br style="box-sizing: border-box;"></div></div><div class="cm-layer cm-layer-above cm-cursorLayer" aria-hidden="true" style="box-sizing: border-box; position: absolute; left: 0px; top: 0px; contain: size style; pointer-events: none; z-index: 150; animation-duration: 1200ms;"></div><div class="cm-layer cm-selectionLayer" aria-hidden="true" style="box-sizing: border-box; position: absolute; left: 0px; top: 0px; contain: size style; z-index: -2;"></div></div></div></div></div></div></ne-card></ne-hole><ne-oli index-type="0" style="box-sizing: border-box; display: flex; justify-content: flex-start; align-items: baseline; padding-left: 2em; margin-bottom: 3.915px; margin-right: var(--viewer-center-align-right);"><ne-oli-i style="box-sizing: border-box; min-width: 2em; margin-left: -2em; display: inline-flex; justify-content: flex-end; user-select: none; white-space: nowrap;"><span class="ne-list-symbol" data-type="0" data-level="0" style="box-sizing: border-box; padding-right: 6px; font-family: "Helvetica Neue", Consolas;"><span style="box-sizing: border-box;">2</span></span></ne-oli-i><ne-oli-c class="ne-oli-content" id="u9e0f88e4" data-lake-id="u9e0f88e4" style="box-sizing: border-box; flex: 1 1 0%; min-width: 1px;"><ne-text id="u63cd09a2" style="box-sizing: border-box;">随机时间函数 随机时间()
</ne-text><ne-text id="ucf5e0370" style="box-sizing: border-box;">功能说明: 这个函数用于生成一个随机的时间,时间格式为 HH:MM:SS。</ne-text><span class="ne-viewer-b-filler" ne-filler="block" style="box-sizing: border-box; display: inline; min-width: 1px; vertical-align: baseline; font-size: 1em; height: 1em; user-select: none;"><br style="box-sizing: border-box;"></span></ne-oli-c></ne-oli><ne-p id="u8fde7f3e" data-lake-id="u8fde7f3e" style="box-sizing: border-box; display: block; min-height: 24px; margin-bottom: 7.83px; margin-top: 7.83px; margin-right: var(--viewer-center-align-right);"><ne-text id="u33b78561" style="box-sizing: border-box;">代码解析:</ne-text><span class="ne-viewer-b-filler" ne-filler="block" style="box-sizing: border-box; display: inline; min-width: 1px; vertical-align: baseline; font-size: 1em; height: 1em; user-select: none;"><br style="box-sizing: border-box;"></span></ne-p><ne-p id="u9a5988b6" data-lake-id="u9a5988b6" style="box-sizing: border-box; display: block; min-height: 24px; margin-bottom: 7.83px; margin-right: var(--viewer-center-align-right);"><ne-text id="u82c13391" style="box-sizing: border-box;">var hours = Math.floor(Math.random() * 24);
</ne-text><ne-text id="u0d9541d1" style="box-sizing: border-box;">生成一个0到23之间的随机整数,表示小时。</ne-text><span class="ne-viewer-b-filler" ne-filler="block" style="box-sizing: border-box; display: inline; min-width: 1px; vertical-align: baseline; font-size: 1em; height: 1em; user-select: none;"><br style="box-sizing: border-box;"></span></ne-p><ne-p id="u887f5164" data-lake-id="u887f5164" style="box-sizing: border-box; display: block; min-height: 24px; margin-bottom: 7.83px; margin-right: var(--viewer-center-align-right);"><ne-text id="u0be6936b" style="box-sizing: border-box;">var minutes = Math.floor(Math.random() * 60);
</ne-text><ne-text id="u741ca756" style="box-sizing: border-box;">生成一个0到59之间的随机整数,表示分钟。</ne-text><span class="ne-viewer-b-filler" ne-filler="block" style="box-sizing: border-box; display: inline; min-width: 1px; vertical-align: baseline; font-size: 1em; height: 1em; user-select: none;"><br style="box-sizing: border-box;"></span></ne-p><ne-p id="uec97e259" data-lake-id="uec97e259" style="box-sizing: border-box; display: block; min-height: 24px; margin-bottom: 7.83px; margin-right: var(--viewer-center-align-right);"><ne-text id="u1929e214" style="box-sizing: border-box;">var seconds = Math.floor(Math.random() * 60);
</ne-text><ne-text id="u360926bc" style="box-sizing: border-box;">生成一个0到59之间的随机整数,表示秒数。</ne-text><span class="ne-viewer-b-filler" ne-filler="block" style="box-sizing: border-box; display: inline; min-width: 1px; vertical-align: baseline; font-size: 1em; height: 1em; user-select: none;"><br style="box-sizing: border-box;"></span></ne-p><ne-p id="u4b49ce90" data-lake-id="u4b49ce90" style="box-sizing: border-box; display: block; min-height: 24px; margin-bottom: 7.83px; margin-right: var(--viewer-center-align-right);"><ne-text id="u31f017a3" style="box-sizing: border-box;">hours = hours < 10 ? '0' + hours : hours;
</ne-text><ne-text id="u15f7c373" style="box-sizing: border-box;">如果小时数小于10,则在前面补0,使其保持两位数格式。</ne-text><span class="ne-viewer-b-filler" ne-filler="block" style="box-sizing: border-box; display: inline; min-width: 1px; vertical-align: baseline; font-size: 1em; height: 1em; user-select: none;"><br style="box-sizing: border-box;"></span></ne-p><ne-p id="uf646ee6e" data-lake-id="uf646ee6e" style="box-sizing: border-box; display: block; min-height: 24px; margin-bottom: 7.83px; margin-right: var(--viewer-center-align-right);"><ne-text id="u92866ac0" style="box-sizing: border-box;">minutes = minutes < 10 ? '0' + minutes : minutes;
</ne-text><ne-text id="u4ad3c5ac" style="box-sizing: border-box;">如果分钟数小于10,则在前面补0,使其保持两位数格式。</ne-text><span class="ne-viewer-b-filler" ne-filler="block" style="box-sizing: border-box; display: inline; min-width: 1px; vertical-align: baseline; font-size: 1em; height: 1em; user-select: none;"><br style="box-sizing: border-box;"></span></ne-p><ne-p id="u80abf2aa" data-lake-id="u80abf2aa" style="box-sizing: border-box; display: block; min-height: 24px; margin-bottom: 7.83px; margin-right: var(--viewer-center-align-right);"><ne-text id="u8941cc52" style="box-sizing: border-box;">seconds = seconds < 10 ? '0' + seconds : seconds;
</ne-text><ne-text id="ubf466abc" style="box-sizing: border-box;">如果秒数小于10,则在前面补0,使其保持两位数格式。</ne-text><span class="ne-viewer-b-filler" ne-filler="block" style="box-sizing: border-box; display: inline; min-width: 1px; vertical-align: baseline; font-size: 1em; height: 1em; user-select: none;"><br style="box-sizing: border-box;"></span></ne-p><ne-p id="u65e1351e" data-lake-id="u65e1351e" style="box-sizing: border-box; display: block; min-height: 24px; margin-bottom: 7.83px; margin-right: var(--viewer-center-align-right);"><ne-text id="ua91ca21c" style="box-sizing: border-box;">return hours + ':' + minutes + ':' + seconds;
</ne-text><ne-text id="u43182c80" style="box-sizing: border-box;">返回格式化后的时间字符串,格式为 HH:MM:SS。</ne-text><span class="ne-viewer-b-filler" ne-filler="block" style="box-sizing: border-box; display: inline; min-width: 1px; vertical-align: baseline; font-size: 1em; height: 1em; user-select: none;"><br style="box-sizing: border-box;"></span></ne-p><ne-p id="ufb37e124" data-lake-id="ufb37e124" style="box-sizing: border-box; display: block; min-height: 24px; margin-bottom: 7.83px; margin-right: var(--viewer-center-align-right);"><ne-text id="ub0fd88e2" style="box-sizing: border-box;">示例调用: printl(随机时间());
</ne-text><ne-text id="u7d4604eb" style="box-sizing: border-box;">这个调用会生成一个随机的时间并打印出来,例如 "14:05:09"。</ne-text><span class="ne-viewer-b-filler" ne-filler="block" style="box-sizing: border-box; display: inline; min-width: 1px; vertical-align: baseline; font-size: 1em; height: 1em; user-select: none;"><br style="box-sizing: border-box;"></span></ne-p><ne-p id="u473e24f4" data-lake-id="u473e24f4" style="box-sizing: border-box; display: block; min-height: 24px; margin-bottom: 7.83px; margin-right: var(--viewer-center-align-right);"><ne-text id="ubf6d1f74" style="box-sizing: border-box;">总结
</ne-text><ne-text id="u468149c4" style="box-sizing: border-box;">随机倒计时(minSeconds, maxSeconds) 函数用于生成一个指定范围内的随机倒计时,并每秒更新剩余时间。
</ne-text><ne-text id="u5bc7b1e3" style="box-sizing: border-box;">随机时间() 函数用于生成一个随机的 HH:MM:SS 格式的时间字符串。</ne-text></ne-p></div></div></div></div></div></article></div></div>
页:
[1]