日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:52010
  • 待審:67
  • 小程序:12
  • 文章:1106242
  • 會(huì)員:784

我最近不得不創(chuàng)建一個(gè)沒(méi)有后端端點(diǎn)的用戶界面(ui)。重點(diǎn)是使 ui 盡可能具有響應(yīng)性,以便用戶可以知道操作何時(shí)正在進(jìn)行。

這主要意味著當(dāng)進(jìn)行 ajax 調(diào)用時(shí),ui 應(yīng)進(jìn)行指示,并在調(diào)用完成時(shí)進(jìn)行相應(yīng)更新。

為了幫助 ui 的開(kāi)發(fā),我創(chuàng)建了一個(gè)函數(shù)來(lái)模擬 ajax 調(diào)用。該功能能夠:

接受延遲(以毫秒為單位)來(lái)模擬進(jìn)行實(shí)際 ajax 調(diào)用的延遲
接受 ajax 調(diào)用失敗時(shí)模擬失敗的概率
返回提供的有效負(fù)載

typescript 代碼如下(請(qǐng)參閱帶有文檔字符串的完整代碼示例的要點(diǎn)):

export async function delay<t>(
  timeout: number,
  probability?: number,
  result?: t
): promise<t> {
  return new promise<t>((resolve, reject) =&gt; {
    settimeout(() =&gt; {
      if (!probability || probability  1) {
        resolve(result);
        return;
      }

      const hit = math.random();
      if (hit 



<p>要使用此功能:<br></p>

<pre class="brush:php;toolbar:false">async function handlebuttonclick() {
  // update the ui to show a loading indicator.

  try {
    // highlight-start
    // make the call take 3 seconds, with a 10% chance of failure,
    // and return an array of users.
    const result = await delay(3000, 0.9, [
      {
        email: '[email protected]',
        username: 'user 1',
      },
    ]);
    // highlight-end

    // update the ui when the call completes succesfully.
  } catch (err: any) {
    // update the ui when the call fails.
  }
}

登錄后復(fù)制

相同函數(shù)的 javascript 版本如下:

export async function delay(timeout, probability, result) {
  return new Promise((resolve, reject) =&gt; {
    setTimeout(() =&gt; {
      if (
        !probability ||
        typeof probability !== 'number' ||
        probability  1
      ) {
        resolve(result);
        return;
      }

      const hit = Math.random();
      console.log(hit, probability);
      if (hit 



<p><em>這篇文章首次發(fā)表于 cheehow.dev</em></p>


          

            
        

登錄后復(fù)制

分享到:
標(biāo)簽:ajax 函數(shù) 占位 調(diào)用
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 52010

    網(wǎng)站

  • 12

    小程序

  • 1106242

    文章

  • 784

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定