繁体   English   中英

为什么带有生成器功能的setTimeout无法触发?

[英]Why is my setTimeout with generator function not firing?

我有一个嵌套的yield call ,我想在我的传奇中触发一个计时器。 但是由于某种原因,计时器不会触发,并且不会调用showModal 知道为什么会这样吗?

function* showModal() {
  yield put(showPermissionsNeededWall('Please sign in', null));
  readTimeLoginBoxShown();
}

export default function* showPermissionWall() {
  yield takeLatest(SHOW_TIMED_LOGIN_WALL, function* () {
    const { auth } = yield select(state => state);

    if (!auth.currentUser) {
      // yield showModal(); // works

      yield call(setTimeout, function* () {
        yield showModal(); // never fires
      }, 1000 * SHOW_LOGIN_WALL_AFTER_IN_SECONDS);
    }
  });
};

首先,您必须直接通过call showModal生成器函数,或者应该put将在takeLatest的帮助下触发showModaltakeLatest

因此,每次您要调用showModal

yield call(showModal);

另一方面,在saga上下文中,您应该使用redux-saga/effects delay 在这种情况下,我将使用以下代码:

yield call(delay, 1000 * SHOW_LOGIN_WALL_AFTER_IN_SECONDS);
yield call(showModal);

所有这些在一起:

import { call, put, takeLatest } from 'redux-saga/effects';

export default function* showPermissionWall() {
  yield takeLatest(SHOW_TIMED_LOGIN_WALL, function* () {
    const { auth } = yield select(state => state);

    if (!auth.currentUser) {
      yield call(delay, 1000 * SHOW_LOGIN_WALL_AFTER_IN_SECONDS);
      yield call(showModal);
    }
  });
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM