Javascript 以编程方式在x秒后使用setInterval单击href链接
我有一个简单的3页演示网站,我想尝试以类似于Microsoft Powerpoint演示文稿的方式显示它 因此,当用户手动单击主页上的“开始”链接时,他们会移动到第2页,该页上包含一个“4”(秒)的值。该值插入到页面中的setInterval函数中,该函数在4秒后自动单击页面上的“下一步”链接,并将用户带到第3页。第3页在“6”秒后返回主页(该值再次插入页面内的setInterval函数) 以下是设置间隔计时器代码:Javascript 以编程方式在x秒后使用setInterval单击href链接,javascript,html,Javascript,Html,我有一个简单的3页演示网站,我想尝试以类似于Microsoft Powerpoint演示文稿的方式显示它 因此,当用户手动单击主页上的“开始”链接时,他们会移动到第2页,该页上包含一个“4”(秒)的值。该值插入到页面中的setInterval函数中,该函数在4秒后自动单击页面上的“下一步”链接,并将用户带到第3页。第3页在“6”秒后返回主页(该值再次插入页面内的setInterval函数) 以下是设置间隔计时器代码: setInterval(function () { document.
setInterval(function () {
document.querySelector(".o-controls".click();
}, 3000)
以下是3页的代码:
<!--Homepage-->
<html lang="en">
<head>
</head>
<body>
<section id="start">
<p>Click "Start" manually to start a Powerpoint style on pages 2 and 3</p>
<a href="/page2.html">Start</a>
</section>
</body>
</html>
<!--Page 2-->
<html lang="en">
<head>
</head>
<body>
<section id="page2">
<div class="wrapper">
<p>Javascript function clicks "Next" link after value in seconds below:</p>
<p id="secondsValue">4</p>
</div>
<a class="nextPage" href="/page3.html">Next</a>
</section>
<!-- Insert the #secondsValue into the setInterval function, rather than it's current static 4000 value-->
setInterval(function () {
document.querySelector("a.nextPage".click();
}, 4000)
</body>
</html>
<!--Page 3-->
<html lang="en">
<head>
</head>
<body>
<section id="page2">
<div class="wrapper">
<p>Javascript function clicks "Next" link after value in seconds below. This then loops you back to the Homepage.</p>
<p id="secondsValue">6</p>
</div>
<a class="nextPage" href="/homepage.html">Next</a>
</section>
<!-- Insert the #secondsValue into the setInterval function, rather than it's current static 6000 value-->
setInterval(function () {
document.querySelector("a.nextPage".click();
}, 6000)
</body>
</html>
手动单击“开始”,在第2页和第3页上开始Powerpoint样式
Javascript函数在以下以秒为单位的值后单击“下一步”链接:
4
setInterval(函数(){
document.querySelector(“a.nextPage.”单击();
}, 4000)
Javascript函数在下面以秒为单位的值后单击“下一步”链接,然后将您返回主页
6
setInterval(函数(){
document.querySelector(“a.nextPage.”单击();
}, 6000)
如何将页面上某个元素中存储的秒数值插入到setInterval函数中?使用
querySelector
和.innerText
获取值,并使用parseInt()
将其转换为整数,然后将其相乘:
var seconds = parseInt(document.querySelector("#secondsValue").innerText)*1000;
setInterval(function () {
document.querySelector("a.nextPage").click();
}, seconds)
使用
querySelector
和和
获取值,并使用parseInt()
将其转换为整数并在以下操作后相乘:
var seconds = parseInt(document.querySelector("#secondsValue").innerText)*1000;
setInterval(function () {
document.querySelector("a.nextPage").click();
}, seconds)
setInterval(…,Number(document.querySelector('#secondsValue').textContent)*1000)
缺少一个右括号。document.querySelector(“a.nextPage”)。单击();
setInterval(…,Number(document.querySelector('#secondsValue').textContent)*1000)缺少右括号。document.querySelector(“a.nextPage”)。单击()
谢谢niklaz,那4000
值不应该是动态值,而不是静态值吗?因为它是#secondsValue
@CWilliamson中值变化的结果,抱歉,是的,我已经修复了代码。谢谢niklaz,4000
值不应该是动态值,而不是静态值吗?因为它是变化的结果在#secondsValue
@CWilliamson中输入值,抱歉,是的,我已经修复了代码。