英文:
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请求到服务器。然而,出于性能原因,这只有在出于安全或用户体验原因(跨浏览器和设备兼容性)时才应该这样做。
您需要做的事情:
- 为您的 "button" 元素(图像?)添加一个
eventListener
来检查click
事件。 - 单击函数使用
setTimeout
函数运行一个计时器。 - 在您的视频标签设置中启用
loop
。 - 在
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:
- Add an
eventListener
to your "button" element (image?) to check forclick
events. - The click function runs a Timer using
setTimeout
function. - Enable
loop
in your video tag setup. - Use a
location.href
method within thesetTimeout
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论