Javascript setTimeout 返回什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10068981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:42:59  来源:igfitidea点击:

What does setTimeout return?

javascript

提问by u283863

I was curious that what does setTimeout return. So I did a quick test:

我很好奇 setTimeout 返回什么。所以我做了一个快速测试:

var thing = setTimeout(function(){},1);

And what surprise me is that it gave me a number. 1351Each time is different.

令我惊讶的是,它给了我一个数字。1351每一次都不一样。

So is it really all it returns is a number? So I can actually do this as well?

那么它返回的真的只是一个数字吗?所以我真的可以这样做吗?

clearTimeout(1351);

Very confusing...

非常混淆...

回答by rid

It's a handle (a unique identifier). When you create a timeout, the JavaScript runtime associates a handle with the timeout you created, and it can identify that timeout by the handle setTimeout()returns. When you run clearTimeout(), it will know what timeout you're talking about by looking at the unique handle you pass in.

它是一个句柄(唯一标识符)。当您创建超时时,JavaScript 运行时将一个句柄与您创建的超时相关联,它可以通过句柄setTimeout()返回来识别该超时。当你运行时clearTimeout(),它会通过查看你传入的唯一句柄来知道你在谈论什么超时。

回答by Andrew_1510

It can be an Object, I tested it with node.js:

它可以是一个Object,我用node.js以下方法测试过:

var sto = setTimeout(
    function(){console.log('ping');}, 
    1000
);

console.log(sto);

The output is:

输出是:

{ _idleTimeout: 1000,
  _idlePrev:
   { '0': [Function: listOnTimeout],
     _idleNext: [Circular],
     _idlePrev: [Circular],
     msecs: 1000 },
  _idleNext:
   { '0': [Function: listOnTimeout],
     _idleNext: [Circular],
     _idlePrev: [Circular],
     msecs: 1000 },
  _idleStart: 2413359232,
  _onTimeout: [Function],
  _repeat: false,
  domain:
   { domain: null,
     _events: { error: [Function] },
     _maxListeners: undefined,
     members: [] } }

回答by louis.luo

You can think of it as a timerID, which uniquely identify a timer, so that you can reset by clearTimeout(timerID)

您可以将其视为一个timerID,它唯一标识一个计时器,以便您可以通过重置clearTimeout(timerID)

回答by louis.luo

it's a task ID (I prefer to think it's a task_ID rather than a Timeout_ID that confuses everyone).

它是一个任务 ID(我更愿意认为它是一个 task_ID 而不是一个让每个人都感到困惑的 Timeout_ID)。

imagine you have to launch a default function : "f_cup_of_tea", that any normal individual would end up needing after 5 minutes on your website. :

想象一下,您必须启动一个默认功能:“f_cup_of_tea”,任何普通人在您的网站上停留 5 分钟后最终都会需要该功能。:

tea_Task_ID = window.setTimeout(f_cup_of_tea, (5*60*1000), 15, 2);

like: function f_cup_of_tea(milk_ml, sugar) { .... }

像:函数 f_cup_of_tea(milk_ml, Sugar) { .... }

but unfortunatly, with ten second late, a psychadelic user prefer to get something different than the entire world, and choose a bad_tequila...

但不幸的是,迟到了十秒,一个精神错乱的用户更喜欢得到与整个世界不同的东西,并选择了坏龙舌兰酒......

you must cancel the delicious " f_cup_of_tea " scheduled within five minutes ... fortunately javascript have thinking of this kind of problem and you can use :

您必须在五分钟内取消预定的美味“f_cup_of_tea”......幸运的是javascript已经想到了这种问题,您可以使用:

window.clearTimeout(tea_Task_ID);

(but it woorks only if " f_cup_of_tea " are not yet started).

(但仅当“ f_cup_of_tea ”尚未启动时才有效)。

next, you can launch :

接下来,您可以启动:

tequila_Task_ID = window.setTimeout(f_bad_tequila, (5*1000), 0);  // for in 5s, with zero ice...