如何更新状态以使我的计时器在视频加载后运行

发布于 2025-01-26 00:03:01 字数 1710 浏览 1 评论 0原文

我是React的新手。我有点困扰问题,需要了解一些有关状态更新的指针。

问题:我正在加载视频,并添加“跳过”按钮以从屏幕上删除视频。视频将通过模式下的HTML播放。问题是我的视频负载在页面刷新和模态将在加载视频之前打开后的2或3秒内。我将计时器设置为5秒钟,该计时器的反向格式,例如5、4、3、2、1,跳过,

但由于视频负载缓慢,计时器在后台运行,当视频显示时,有时会看到3, 2,1,跳过

我要寻找的是当我的视频启动加载时,只有我的计时器函数触发。

我尝试过Onload(),onstart()将标志状态从false到true和componentDidmount->根据状态加载页面,但没有运气,

这是我的代码:

export default class FullScreenAds extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isFullPageVideoModalEnabled: false,
      timerForVideo: 7,
      isDisabled: true,
    };
  },
  async componentDidMount() {
    const uuid = Math.round((new Date()).getTime() / 1000);
    let getUserSession = localStorage.getItem("gid");
    if (getUserSession === null || getUserSession.length < 1) {
      localStorage.setItem("gid", uuid);
    }
    this.setState({
      isFullPageVideoModalEnabled: true,
      getSession: getUserSession
    })
    this.startTimer()
  },
  async startTimer() {
    // await sleep(3000);
    this.timer = setInterval(() => {
      const { timerForVideo } = this.state

      if (timerForAds > 0) {
        this.setState(({ timerForVideo }) => ({
          timerForVideo: timerForVideo - 1
        }))
      }
      if (timerForAds === 0) {
        this.setState({ isDisabled: false })
        clearInterval(this.timer)
      }
    }, 1000)
  }

  componentWillUnmount() {
    clearInterval(this.timer)
  }
  render() {
    return (
      // run the modal based on state and load the video
    <video  width="100%" height="auto" autoPlay playsInline loop>
        <source type="video/mp4" src="video/src/url" />
    </video>
    )
  }
}

I am newbie on react. I am bit stuck in problem and need to know some pointers regarding state update.

PROBLEM: I am making the video on load and add the skip button for removing the video from screen. Video will be played through HTML under the modal. Problem is my video load in 2 or 3 sec after the page refresh and modal will open prior to load video. I have set the timer from 5 seconds which is in reverse format like 5, 4, 3, 2, 1, skip

But due to slow/late video load, timer run in background and in front side when video display sometime it sees 3, 2, 1, skip

What I am looking for is when my video start load then only my timer function trigger.

I have tried onLoad(), onStart() to update the flag state from false to true and on componentDidMount -> load the page according to the state but not getting luck

This is my code:

export default class FullScreenAds extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isFullPageVideoModalEnabled: false,
      timerForVideo: 7,
      isDisabled: true,
    };
  },
  async componentDidMount() {
    const uuid = Math.round((new Date()).getTime() / 1000);
    let getUserSession = localStorage.getItem("gid");
    if (getUserSession === null || getUserSession.length < 1) {
      localStorage.setItem("gid", uuid);
    }
    this.setState({
      isFullPageVideoModalEnabled: true,
      getSession: getUserSession
    })
    this.startTimer()
  },
  async startTimer() {
    // await sleep(3000);
    this.timer = setInterval(() => {
      const { timerForVideo } = this.state

      if (timerForAds > 0) {
        this.setState(({ timerForVideo }) => ({
          timerForVideo: timerForVideo - 1
        }))
      }
      if (timerForAds === 0) {
        this.setState({ isDisabled: false })
        clearInterval(this.timer)
      }
    }, 1000)
  }

  componentWillUnmount() {
    clearInterval(this.timer)
  }
  render() {
    return (
      // run the modal based on state and load the video
    <video  width="100%" height="auto" autoPlay playsInline loop>
        <source type="video/mp4" src="video/src/url" />
    </video>
    )
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左耳近心 2025-02-02 00:03:01

使用react.CreateEf()创建参考,然后将参考附加到视频中,然后在视频中聆听onplay事件,然后在OnPlay事件启动时启动计时器:

尝试以下操作:

export default class FullScreenAds extends React.Component {
  constructor(props) {
    super(props);
    this.videoRef = React.createRef()
    this.state = {
      isFullPageVideoModalEnabled: false,
      timerForVideo: 7,
      isDisabled: true,
    };
  },
  async componentDidMount() {
    const uuid = Math.round((new Date()).getTime() / 1000);
    let getUserSession = localStorage.getItem("gid");
    if (getUserSession === null || getUserSession.length < 1) {
      localStorage.setItem("gid", uuid);
    }
    this.setState({
      isFullPageVideoModalEnabled: true,
      getSession: getUserSession
    })
    this.videoRef.current.onplay = () => this.startTimer();
  },
  async startTimer() {
    // await sleep(3000);
    this.timer = setInterval(() => {
      const { timerForVideo } = this.state

      if (timerForAds > 0) {
        this.setState(({ timerForVideo }) => ({
          timerForVideo: timerForVideo - 1
        }))
      }
      if (timerForAds === 0) {
        this.setState({ isDisabled: false })
        clearInterval(this.timer)
      }
    }, 1000)
  }

  componentWillUnmount() {
    clearInterval(this.timer)
  }
  render() {
    return (
      // run the modal based on state and load the video
    <video ref={this.videoRef} width="100%" height="auto" autoPlay playsInline loop>
        <source type="video/mp4" src="video/src/url" />
    </video>
    )
  }
}

Create a reference with React.createRef() and attach the reference to the video, then listen for the onplay event on the video and start the timer when the onplay event fires:

Try this:

export default class FullScreenAds extends React.Component {
  constructor(props) {
    super(props);
    this.videoRef = React.createRef()
    this.state = {
      isFullPageVideoModalEnabled: false,
      timerForVideo: 7,
      isDisabled: true,
    };
  },
  async componentDidMount() {
    const uuid = Math.round((new Date()).getTime() / 1000);
    let getUserSession = localStorage.getItem("gid");
    if (getUserSession === null || getUserSession.length < 1) {
      localStorage.setItem("gid", uuid);
    }
    this.setState({
      isFullPageVideoModalEnabled: true,
      getSession: getUserSession
    })
    this.videoRef.current.onplay = () => this.startTimer();
  },
  async startTimer() {
    // await sleep(3000);
    this.timer = setInterval(() => {
      const { timerForVideo } = this.state

      if (timerForAds > 0) {
        this.setState(({ timerForVideo }) => ({
          timerForVideo: timerForVideo - 1
        }))
      }
      if (timerForAds === 0) {
        this.setState({ isDisabled: false })
        clearInterval(this.timer)
      }
    }, 1000)
  }

  componentWillUnmount() {
    clearInterval(this.timer)
  }
  render() {
    return (
      // run the modal based on state and load the video
    <video ref={this.videoRef} width="100%" height="auto" autoPlay playsInline loop>
        <source type="video/mp4" src="video/src/url" />
    </video>
    )
  }
}
始终不够 2025-02-02 00:03:01

或者,您可以尝试将功能组件与钩子一起使用。

这样的事情:

export const FullScreenAds = () => {
  const [state, setState] = React.useState({
    isFullPageVideoModalEnabled: false,
    timerForVideo: 7,
    isDisabled: true,
  });
  const videoRef = React.useRef();
  const timerRef = React.useRef();

  const startTimer = () => {
    timerRef.current = setInterval(() => {
      const { timerForVideo } = state;

      if (timerForAds > 0) {
        setState({
          ...state,
          timerForVideo: state.timerForVideo - 1,
        });
      }
      if (timerForAds === 0) {
        setState({ ...state, isDisabled: false });
        clearInterval(this.timer);
      }
    }, 1000);
  };

  useEffect(() => {
    const uuid = Math.round(new Date().getTime() / 1000);
    let getUserSession = localStorage.getItem("gid");

    if (getUserSession === null || getUserSession.length < 1) {
      localStorage.setItem("gid", uuid);
    }

    setState({
      ...state,
      isFullPageVideoModalEnabled: true,
      getSession: getUserSession,
    });

    videoRef.current.onplay = startTimer;

    return clearInterval(timerRef.current);
  }, []);

  return (
    <video
      ref={this.videoRef}
      width="100%"
      height="auto"
      autoPlay
      playsInline
      loop
    >
      <source type="video/mp4" src="video/src/url" />
    </video>
  );
};

Or you can try using functional component with hooks.

Something like this:

export const FullScreenAds = () => {
  const [state, setState] = React.useState({
    isFullPageVideoModalEnabled: false,
    timerForVideo: 7,
    isDisabled: true,
  });
  const videoRef = React.useRef();
  const timerRef = React.useRef();

  const startTimer = () => {
    timerRef.current = setInterval(() => {
      const { timerForVideo } = state;

      if (timerForAds > 0) {
        setState({
          ...state,
          timerForVideo: state.timerForVideo - 1,
        });
      }
      if (timerForAds === 0) {
        setState({ ...state, isDisabled: false });
        clearInterval(this.timer);
      }
    }, 1000);
  };

  useEffect(() => {
    const uuid = Math.round(new Date().getTime() / 1000);
    let getUserSession = localStorage.getItem("gid");

    if (getUserSession === null || getUserSession.length < 1) {
      localStorage.setItem("gid", uuid);
    }

    setState({
      ...state,
      isFullPageVideoModalEnabled: true,
      getSession: getUserSession,
    });

    videoRef.current.onplay = startTimer;

    return clearInterval(timerRef.current);
  }, []);

  return (
    <video
      ref={this.videoRef}
      width="100%"
      height="auto"
      autoPlay
      playsInline
      loop
    >
      <source type="video/mp4" src="video/src/url" />
    </video>
  );
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文