为什么 setInterval() 会给出循环引用? [英] Why does setInterval() give a circular reference?

查看:35
本文介绍了为什么 setInterval() 会给出循环引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

let id = setInterval(function f() {console.log("Nice.");}, 1000);
console.log(id);
JSON.stringify(id);

最后一行出现错误,内容为TypeError: Converting circle structure to JSON.

This ends in an error on the last line saying TypeError: Converting circular structure to JSON.

console.log(id) 给出

Timeout {
  _called: false,
  _idleTimeout: 1000,
  _idlePrev: 
   TimersList {
     _idleNext: [Circular],
     _idlePrev: [Circular],
     _timer: Timer { '0': [Function: listOnTimeout], _list: [Circular] },
     _unrefed: false,
     msecs: 1000,
     nextTick: false },
  _idleNext: 
   TimersList {
     _idleNext: [Circular],
     _idlePrev: [Circular],
     _timer: Timer { '0': [Function: listOnTimeout], _list: [Circular] },
     _unrefed: false,
     msecs: 1000,
     nextTick: false },
  _idleStart: 81,
  _onTimeout: [Function: f],
  _timerArgs: undefined,
  _repeat: 1000,
  _destroyed: false,
  [Symbol(asyncId)]: 6,
  [Symbol(triggerAsyncId)]: 1 }

为什么 Timeout 对象包含循环引用?

Why does the Timeout object contain a circular reference?

推荐答案

请注意,setInterval 的行为不同于 setInterval.虽然 javascript 版本返回一个整数,但 node.js 返回一个 Timeout 对象. 并且是不用于序列化.

Please note, that setInterval in node.js behaves differently than setInterval in javascript. While the javascript version returns an integer, node.js returns a Timeout object. and is not intended for serialization.

这篇关于为什么 setInterval() 会给出循环引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆