Python Flask还是JavaScript?

huangapple go评论88阅读模式
英文:

Python Flask or Javascript?

问题

你可以在HTML5中使用如下代码来实现:

<video autoplay loop muted poster="your-poster-image.jpg">
  <source src="your-video.mp4" type="video/mp4">
  Your browser does not support the video tag.
</video>

<button onclick="playVideo()">Play Video</button>

<script>
function playVideo() {
  var video = document.querySelector('video');
  video.play();
  setTimeout(function() {
    window.location.href = 'your-redirect-url.html';
  }, 5000); // 5000 milliseconds (5 seconds) delay before redirect
}
</script>

在这段代码中,你可以将 "your-poster-image.jpg" 替换为视频加载时显示的图片,"your-video.mp4" 替换为你要播放的视频文件,"Play Video" 是按钮上显示的文本,"your-redirect-url.html" 是页面将要重定向到的网页地址,"5000" 是延迟的时间,单位是毫秒。你可以根据需要调整这些值。

英文:

How can I get a button in HTML5 which when pressed plays a loop video in background and after some time redirects the page to another webpage?

答案1

得分: 1

这里有JS和Python之间的巨大区别:

Python是服务器端的,因此在服务器上运行。
JavaScript是客户端的,运行在浏览器内。

Web服务器仅处理请求。出于明显的安全原因,它无法控制或访问浏览器。

这意味着服务器将永远不会知道用户是否按下了按钮,因为它无法监听它。您需要在客户端添加一个事件监听器来检查这一点。然后,您可以使用客户端脚本发送POST请求到服务器。然而,出于性能原因,这只有在出于安全或用户体验原因(跨浏览器和设备兼容性)时才应该这样做。

您需要做的事情:

  1. 为您的 "button" 元素(图像?)添加一个 eventListener 来检查 click 事件。
  2. 单击函数使用 setTimeout 函数运行一个计时器
  3. 在您的视频标签设置中启用 loop
  4. setTimeout 中使用 location.href 方法在时间耗尽时进行重定向。
英文:

There is a huge difference between JS and Python:

Python is server-sided and as such run on the server.
JavaScript is client-sided and runs within the browser.

Webservers only work with requests. For obvious security reasons, it can not control or access a browser.

That means a server will never know if a user pressed a button because he has no control to listen to it. You need a client-sided eventListener to check for it. Then you can use a client-side script to send a post request to the server. However, for performance reasons, it should only be done if it is necessary for security reasons or UX (cross-browser and device savings).

What you need to do:

  1. Add an eventListener to your "button" element (image?) to check for click events.
  2. The click function runs a Timer using setTimeout function.
  3. Enable loop in your video tag setup.
  4. Use a location.href method within the setTimeout to redirect when the time runs out.

答案2

得分: 0

如果你需要一个按钮,你需要使用Html将它显示在屏幕上,然后使用JavaScript播放循环视频,再经过一段延迟后进行重定向。这些都需要使用JavaScript来完成。你应该使用事件监听器来绑定视频,在点击后播放,然后添加setTimeout来等待一段时间,最后使用window.location.replace(...)来进行重定向。你可以在这里查看更详细的解释:https://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage

英文:

If you're in need of a button you need to use Html to show it on screen and then use JavaScript to play the looping video and after some delay to be redirected. You'll surely need JavaScript to do these. You should use event listeners and bind the video to be played after clicking and then add setTimeOut to wait for sometime and then use window.location.replace(...) to redirect. Here you can check for better explanation https://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage

huangapple
  • 本文由 发表于 2023年2月27日 18:09:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定