var interval:number ;
function start() {
interval = setInterval(function() {
console.log(interval)
console.log("interval...");
}, 1000);
}
function stop() {
console.log("stopped");
console.log(interval)
clearInterval(interval);
interval = 0;
console.log("stopped end");
}
function showInterval(){
console.log(interval)
}
...
return(
<View>
<Button title="startBtn" onPress={()=>{start();}}/>
<Button title="stopBtn" onPress={()=>{stop();}}/>
<Button title="showIntervalBtn" onPress={()=>{showInterval();}}/>
</View>
)
"I expected that when I click the stopBtn and showIntervalBtn, it would clear the interval using clearInterval(interval) and display the interval value using console.log(interval). However, the console displayed '0' and the stopBtn did not work. The start() function still displayed a unique interval value. What am I missing?"
I tried 'useState' and 'useRef'. they are good working.