在 Ionic3 中,我设置了一个刷新数据的间隔,我希望它在离开视图时停止。但我无法确定间隔实例的范围...请注意,这是我打字稿的第一步...
updateData(){
var interval = setInterval(() => {
this.getdata.getData().subscribe( data => {
this.channels = data.channels;
})
},2000);
}
ionViewWillLeave() {
clearInterval(interval);//[TS]Cannot find name 'interval'
console.log('leave view');
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试更改每 1 秒显示的图像,第一张图像出现然后切换到 alt 显示并且不继续切换图片
export default class Slideshow extends Component {
constructor(props) {
super(props);
this.getImageId = this.getImageId.bind(this);
this.switchImage = this.switchImage.bind(this);
this.init = this.init.bind(this);
this.state = {
currentImage: 0,
image: 0
};
}
getImageId() {
if(this.currentImage < 3) {
this.setState({
currentImage: this.state.currentImage +1
})
} else {
this.setState({
currentImage: 0
})
}
return this.currentImage;
}
switchImage() {
this.setState({
image: this.getImageId()
});
}
init() {
setInterval(this.switchImage, 1000)
}
render() {
const imagePath = [guy, girl, wash, swifer];
this.init();
return (
<div …Run Code Online (Sandbox Code Playgroud) 我有以下循环:
for (let index = 0; index < 4; index++) {
setInterval(function() {
console.log(index)
}, 1000);
}Run Code Online (Sandbox Code Playgroud)
我怎样才能让它控制台记录第一秒 0,第二秒 1,第三秒 2,第四秒 3,第五秒 0,依此类推,直到间隔被清除?
语境:
问题:
function useInterval(callback, delay) {
const savedCallback = React.useRef();
// Remember the latest callback.
React.useEffect(() => {
savedCallback.current = callback;
}, [callback]);
// Set up the interval.
React.useEffect(() => {
function tick() {
savedCallback.current();
}
if (delay !== null) {
let id = setInterval(tick, delay);
return () => clearInterval(id);
}
}, [delay]);
}
const defaultMessages = [
{
message: "message 1",
new: false
},
{
message: "message 2",
new: …Run Code Online (Sandbox Code Playgroud) 我有这个非常简单的应用程序,我正在尝试实现倒计时,Start并且Stop功能似乎工作正常,但是当我按 时Stop, 的值seconds没有在视图中更新,这是所需的行为,但是当我观察控制台日志时,它显示sec我猜不断变化的值表明该值setInterval仍在运行且未清除。
这是附带的代码:
import React, { useState, useEffect } from "react";
import {
StyleSheet,
Text,
View,
StatusBar,
TouchableOpacity,
Dimensions,
} from "react-native";
const screen = Dimensions.get("screen");
export default function App() {
const [seconds, setSeconds] = useState(4);
const [start, setStartToggle] = useState(true);
let [fun, setFun] = useState(null);
const getRemaining = () => {
const minute = Math.floor(seconds / 60);
const second = seconds - minute * 60;
return …Run Code Online (Sandbox Code Playgroud) 我有一个计数器(React hooks 组件),它每秒递增地呈现一个新数字。当钩子更新时,如何断言 DOM 中存在某个数字?
这是代码沙箱链接
import React, { useState, useEffect } from "react";
export default function Counter() {
const [count, setCount] = useState(1);
useEffect(() => {
const intervalId = setInterval(function () {
setCount(count + 1);
}, 1000);
return () => clearInterval(intervalId);
});
return <span>{count}</span>;
}
Run Code Online (Sandbox Code Playgroud)
测试失败
test("should be able to find 3 directly", async () => {
render(<Counter />);
const three = await waitFor(() => screen.findByText(/3/i));
expect(three).toBeInTheDocument();
});
Run Code Online (Sandbox Code Playgroud)
通过测试
test("should render one and then two and then three", …Run Code Online (Sandbox Code Playgroud) javascript unit-testing setinterval reactjs react-testing-library
我正在制作一个 React 仪表板,每分钟都会调用 API 进行更新。根据SO中的许多答案,我现在有这样的作品:
const Dashboard = (props) => {
const [stockData, setStockData] = useState([]);
useEffect(() => {
//running the api call on first render/refresh
getAPIData();
//running the api call every one minute
const interval = setInterval(() => {
getAPIData()
}, 60000);
return () => clearInterval(interval);
}, []);
//the api data call
const getAPIData = async () => {
try {
const stdata = await DataService.getStockData();
setStockData(stdata);
}
catch (err) {
console.log(err);
}
};
Run Code Online (Sandbox Code Playgroud)
但是我不断收到浏览器警告
React Hook useEffect has …Run Code Online (Sandbox Code Playgroud) setInterval( function returnval(){
console.log('hello world');
return returnval;
}(),2000);
Run Code Online (Sandbox Code Playgroud)
首先,请记住我是 javascript 新手。有人可以解释这段让我困惑的代码吗?当我们在匿名 setInterval 中包含的 IIFE 函数中返回函数名称时,实际上发生了什么?还要提前谢谢你。
<?php
echo '
<script type="text/javascript">
$(function() {
var refresh = setInterval(function() {
$("#content").html("'.rand().'");
}, 3000);
});
</script>
<div id="content"></div>
';
?>
Run Code Online (Sandbox Code Playgroud)
这将在3秒后用随机数更新div"内容",但它只更新一次.为什么它不会每三秒钟不断生成一个新数字,我怎样才能让它完全按照这个数字来做?
提前感谢您提供的任何帮助.
我有三个函数:get_stat(sess),它接受一个参数发送到php脚本handlestat(),它处理php脚本check_sms(sess_a)的响应,它应该使用setInterval重复自身 - 它依赖于一个变量disabled_stat来清除计时器
但它不起作用,get_stat(sess)没有被解雇,它只是停滞不前
var disabled_stat = false;
function get_stat(sess)
{
if(disabled_stat==false)
{
var url = "/sms_check_status.php?param=";
var title_f = document.getElementById('stat_title');
var stat_f = document.getElementById('stat_text');
title_f.innerHTML = ' ';
stat_f.innerHTML = ' ';
var myRandom=parseInt(Math.random()*99999999);
http.open("GET", url + escape(sess) + "&rand=" + myRandom, true);
http.onreadystatechange = handlestat;
http.send(null);
}
}
function handlestat()
{
var str_out = '';
var results = '';
if (http.readyState == 4)
{
results = http.responseText.split("~");
if(results[0]=='1')
{
document.getElementById('stat_title').innerHTML = results[1];
document.getElementById('stat_text').innerHTML = results[2];
if(results[3]=='1')
{
disabled_stat = true; …Run Code Online (Sandbox Code Playgroud) setinterval ×10
javascript ×8
reactjs ×4
arrays ×1
console.log ×1
for-loop ×1
html ×1
iife ×1
ionic3 ×1
jquery ×1
react-hooks ×1
react-native ×1
settimeout ×1
typescript ×1
unit-testing ×1
use-effect ×1