javascript 延迟AJAX请求

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 延迟AJAX请求相关的知识,希望对你有一定的参考价值。

const ofPermissionType = (
  requiredPermissionType,
) => (
  filter(({ permissionType }) => (
    requiredPermissionType === permissionType 
  ))
)

const fetchPermissionsEpic = (
  action$,
  state$,
  { ajax },
) => (
  action$
  .pipe(
    ofType(FETCH_PERMISSIONS),
    map(props => ({
      ...props,
      accessToken: (
        accessTokenSelector(
          state$.value,
        )
      ),
    })),
    mergeMap(({
      accessToken,
      permissionTypes,
    }) => (
      from(permissionTypes)
      .pipe(
        map(permissionType => (
          of(permissionType)
          .pipe(
            takeUntil(
              action$
              .pipe(
                ofType(FETCH_PERMISSIONS),
                switchMap(({ permissionTypes }) => (
                  permissionTypes
                )),
                ofPermissionType(permissionType),
              )
            ),
            switchMap(() => (
              ajax({
                crossDomain: true,
                headers: {
                  'Authorization': `Bearer ${accessToken}`,
                },
                url: (
                  'https://example.com/permissions'
                  .concat('?')
                  .concat('permissionType=')
                  .concat(permissionType)
                ),
              })
              .pipe(
                pluck('response'),
                map(permissionValue => ({
                  permissionType,
                  permissionValue,
                })),
                map(storePermission),
              )
            )),
          )
        )),
        mergeAll(2),
      )
    )),
  )
)

以上是关于javascript 延迟AJAX请求的主要内容,如果未能解决你的问题,请参考以下文章

多个 AJAX 请求相互延迟

尝试向 jQuery AJAX 请求添加延迟

ajax请求响应过长怎么解决

简单AJAX请求JSon数据

jQuery ajax 请求作为延迟 - 包装失败回调

JavaScript学习总结——延迟对象跨域模板引擎弹出层AJAX示例