在每个问题组件上,我试图清除时间.所以在componentWillMount()我将启动计时器,然后componentDidUpdate()我将清除超时.计时器似乎没有工作.计时器到期后,我会将用户推回主页.知道为什么使用clearTimeout()不起作用?
class Questions extends Component {
constructor(props){
super(props);
this.state ={
error: false,
position: null,
redirect: false
}
this.error = this.error.bind(this);
this.errorFalse = this.errorFalse.bind(this);
this.timer = this.timer.bind(this);
}
timer=()=>{
setTimeout(() => {
console.log('this ran')
this.setState({
redirect: true
})
}, 5000)
}
componentWillMount(){
this.setState({
error: false
})
this.timer();
}
componentDidUpdate(prevProps, prevState, snapshot, timer){
console.log('updated')
clearTimeout(this.timer);
}
Run Code Online (Sandbox Code Playgroud) 如何clearTimeout这个功能?
这是我的代码,我试过但不行.
我能怎么做 ?
.................................................. .................................................. .................................................. .......
<form id="fid">
<input type="text" id="input_type" onkeypress="stop()" onkeyup="run()"/>
</form>
<p id="myplace"></p>
<script>
function run() {
$('#myplace').hide();
setTimeout(function(){
$.ajax
(
{
url: 'xx.php',
type: 'POST',
data: $('#fid').serialize(),
cache: false,
success: function (data) {
$('#myplace').show();
$('#myplace').html(data);
}
}
)
}, 3000);
}
function stop() {
clearTimeout();
}
</script>
Run Code Online (Sandbox Code Playgroud) 我有一个带有setTimeout的函数.该函数的第一部分将超时设置为清除,以防它在setTimeout触发之前再次调用它.
clearTimeout不会清除计时器.我已经在调用了setTimeout的函数中的部分之后测试了clearTimeout(spinTimer)并且它可以工作.为什么一开始不起作用?
我已经全局声明了变量:
var spinTimer;
function cakeCubeSpin(whereTo){
clearTimeout(spinTimer);
... do some stuff
switch(whereTo){
case 1:
var spinTimer = setTimeout( function(){
$('#ccPanel4').css(leftReset);
$('#ccPanel2').css(rightReset);
$('#ccPanel3').css(backReset);
$('#ccPanel1').css(frontReset);
$('#cakeCubeWrapper').css(wrapReset);
$('#ccPanel1').addClass('cc-current');
}, ccSpeed);
break;
... more stuff
}
Run Code Online (Sandbox Code Playgroud) 我有一个超时ID的数组.什么是最优雅的方式一次清除所有这些?有比这更有效的风格吗?
waitHandler[1] = setTimeout('doSomethingA()', 2000);
waitHandler[2] = setTimeout('doSomethingB()', 2000);
...
for (var i=1; i < waitHandler.length; i++) {
clearTimeout[i];
}
Run Code Online (Sandbox Code Playgroud)