【问题标题】:returning a function value inside an iterator without exiting in javascript在迭代器中返回一个函数值而不在 javascript 中退出
【发布时间】: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


【解决方案1】:

我的目标是将值 20 传递/返回给我的 index.js

为此,您不需要getValue 函数。随便写

// test.js
export default function test() {
  let x = 20;
  setTimeout(test, 1000)
  return x; // <===
}

// index.js
import test from './test.js';
console.log("test() returned", test());

请注意,在超时期间,您现在将获得该返回值,如果您愿意,可以使用它

// test.js
export default function test() {
  let x = 20;
  setTimeout(() => {
    let res = test();
    console.log("test() in timeout returned", res);
  }, 1000)
  return x;
}

任何其他类似的选项可以每 1 秒向客户端传递值?

为此,您希望使用作为参数传递给test 的回调,并且可以调用以将x 的值传递到哪里——以及在你想要的任何时候——并且根据需要经常传递:

// test.js
export default function test(callback) {
//                           ^^^^^^^^
  let x = 20;
  callback(x); // <===
  setTimeout(() => {
    test(callback);
  }, 1000);
}

// index.js
import test from './test.js';
function gotValue(val) {
  console.log("received ", val);
}
test(gotValue);
//   ^^^^^^^^

【讨论】:

  • 是的,它奏效了;但是,它返回了一次。 .我再也不回来了。 .如果 setTimeout() 的目的是停止操作 1 秒,然后在 1 秒休息后再次执行 test()。
  • @WebsiteIsFun 一个函数不能从同一个调用中多次返回。
  • @lightshadowninja 递归使用setTimeout 没有问题,尤其是当你想调整时间或者想有条件地结束它时。您仍然可以通过存储当前超时的 id 轻松取消它。
  • 我明白了,如果你再次调用它时它不能返回,那么我想这回答了这个问题。 . :) 多谢 。 .无论如何,是否有任何其他选项可以每 1 秒向客户端传递价值?
  • 酷酷的。 .正是我想要的。 .非常感谢。 .
【解决方案2】:

如果你想在每个时间间隔从 test 接收一个值,那么你需要传递一个每次都会执行的回调函数。根本不可能从一个函数多次返回一个值(没有生成器函数,但现在不值得担心)。

const test = (callback) => {
    let x = 20;
    return setInterval(() => callback(x), 1000);
}

// then from elsewhere
// the lambda function will be called with x as 20
// every time the interval fires
const unsub = test((x) => {
   // do something with x
   // unsubscribe when you are ready and the interval will be cleared
   if (condition) {
       clearInterval(unsub);
   }
});

【讨论】:

    【解决方案3】:

    我不完全理解你的问题。

    您的函数中似乎根本没有迭代(循环)。

    我的建议是要么在函数的最后返回,要么让你的函数没有 return 语句,而是将 var 的值更改为所需的类型。如果您想返回多个值,只需在上述两个选项中选择一个包含所有所需值的数组即可。

    【讨论】:

    • 是的,这是我最初的想法;但是,如果我不返回它,我不确定如何将值传递给客户端。谢谢
    猜你喜欢
    • 2022-10-01
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 2013-11-10
    • 1970-01-01
    相关资源
    最近更新 更多