【发布时间】:2017-08-13 23:22:17
【问题描述】:
我不确定我的标题是否符合我的问题,但它就是这样。
我有一个迭代器每 1 秒运行一次,一个函数如下所示:
function getValue(val) {
return val;
}
function test() {
let x = 20;
getValue(x)
setTimeout(()=>{
test()
}, 1000)
}
export default test;
当函数getValue() 被命中时,迭代器停止,因为该函数内部有一个返回值。如何在不退出停止迭代的情况下返回值?有办法吗?非常感谢。
index.js (reactjs)
import test from './test.js';
componentDidMount() {
test()
}
test.js
function getValue(val) {
return val;
}
function test() {
let x = 20;
getValue(x)
setTimeout(()=>{
test()
}, 1000)
}
export default test;
我的目标是将值 20 传递/返回给我的 index.js
【问题讨论】:
-
我在您的代码中没有看到任何iterator。你的意思是?什么“停止”或“退出”,你(想)“返回”到哪里?你的actual problem是什么?
-
你不会把
setTimeout误认为setInterval吗? -
@Bergi - 迭代器是测试函数,测试函数内部的 setTimeout() 将每 1sec 执行一次 test() 。 .你会看到它每 1 秒执行一次。
-
@Chinito - 如果我将 setTimeout() 移到外面,它将不再迭代。
-
@AurelianoFarSuau - 是的。 . . hhhmmm 我认为 setInterval 可以解决问题。 .让我试试 。 .非常感谢。
标签: javascript function iteration