{ "data": { "question": { "questionId": "2733", "questionFrontendId": "2621", "categoryTitle": "JavaScript", "boundTopicId": 2222274, "title": "Sleep", "titleSlug": "sleep", "content": "

Given a positive integer millis, write an asynchronous function that sleeps for millis milliseconds. It can resolve any value.

\n\n

 

\n

Example 1:

\n\n
\nInput: millis = 100\nOutput: 100\nExplanation: It should return a promise that resolves after 100ms.\nlet t = Date.now();\nsleep(100).then(() => {\n  console.log(Date.now() - t); // 100\n});\n
\n\n

Example 2:

\n\n
\nInput: millis = 200\nOutput: 200\nExplanation: It should return a promise that resolves after 200ms.\n
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": "睡眠函数", "translatedContent": "

请你编写一个异步函数,它接收一个正整数参数 millis ,并休眠 millis 毫秒。要求此函数可以解析任何值。

\n\n

 

\n\n

示例 1:

\n\n
\n输入:millis = 100\n输出:100\n解释:\n在 100ms 后此异步函数执行完时返回一个 Promise 对象\nlet t = Date.now();\nsleep(100).then(() => {\n  console.log(Date.now() - t); // 100\n});\n
\n\n

示例 2:

\n\n
\n输入:millis = 200\n输出:200\n解释:在 200ms 后函数执行完时返回一个 Promise 对象\n
\n\n

 

\n\n

提示:

\n\n\n", "isPaidOnly": false, "difficulty": "Easy", "likes": 14, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}", "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number} millis\n * @return {Promise}\n */\nasync function sleep(millis) {\n \n}\n\n/** \n * let t = Date.now()\n * sleep(100).then(() => console.log(Date.now() - t)) // 100\n */", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "async function sleep(millis: number): Promise {\n \n}\n\n\n/** \n * let t = Date.now()\n * sleep(100).then(() => console.log(Date.now() - t)) // 100\n */", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"11.8K\", \"totalSubmission\": \"14.2K\", \"totalAcceptedRaw\": 11778, \"totalSubmissionRaw\": 14238, \"acRate\": \"82.7%\"}", "hints": [ "In Javascript, you can execute code after some delay with the setTimeout(fn, sleepTime) function.", "An async function is defined as function which returns a Promise.", "To create a Promise, you can code new Promise((resolve, reject) => {}). When you want the function to return a value, code resolve(value) inside the callback." ], "solution": null, "status": null, "sampleTestCase": "100", "metaData": "{\n \"name\": \"foobar\",\n \"params\": [\n {\n \"name\": \"millis\",\n \"type\": \"integer\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"manual\": true,\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ]\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "envInfo": "{\"javascript\":[\"JavaScript\",\"

\\u7248\\u672c\\uff1aNode.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n

\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a --harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n

lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n

\\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"

TypeScript 5.1.6<\\/p>\\r\\n\\r\\n

Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n

lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n

\\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}", "book": null, "isSubscribed": false, "isDailyQuestion": false, "dailyRecordStatus": null, "editorType": "CKEDITOR", "ugcQuestionId": null, "style": "LEETCODE", "exampleTestcases": "100\n200", "__typename": "QuestionNode" } } }