在 javascript 上执行超时
Timeout with exec on javascript
我有 2 个正在编写 JS 的文件:学习和测试。在学习中,我正在尝试创建一个将执行以下操作的异步函数:
开发一个名为“qAsync
”
的“函数声明”
Returns 一个包含两个键的对象(通过构造函数构建):
- ‘
doAsync
’: function: returns setTimeout
可以使用async/await语法的函数
- ‘
exec
’:使用doAsync
函数在11.5秒后打印一些东西的函数
- ‘
desc
’:字符串:描述doAsync/exec()正在做什么以及如何使用它
但我不确定我的代码是否正确,因为我的测试文件出于某种原因无法识别我的执行程序。我在 learn.js 上的代码:
function qAsync(){
const doAsync = new doAsync(11500);
this.desc= "in order to wait 11.5 sec, call qAsync which calls asyncFun"
let exec= doAsync.exec(("hello after 11.5 sec"));
return (exec,doAsync);
}
let doAsync = async function (ms) {
return await new Promise(resolve => setTimeout(resolve, ms));
}
在 test.js:
function test4(){
let test = qAsync();
alert("first let us decribe the function:\n" + test.desc);
alert("now we will run exec.");
test.exec(); //doesn't work and calls on built in exec and not mine
}
我怎样才能让我的测试工作,你认为我应该改进我的 qAsync 吗?
回复感谢评论:
learn.js:
function qAsync(){
this.doAsync = function (ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
this.desc = "in order to wait 11.5 sec, call exec which calls doAsync, and see the print in the console"
this.exec = async function () {
await this.doAsync(11500);
console.log("did you see this after 11.5 sec?");
}
}
test.js:
async function test4(){
let test = new qAsync();
alert("first let us describe the function:\n"+test.desc);
alert("now we will run exec.");
await test.exec();
}
我有 2 个正在编写 JS 的文件:学习和测试。在学习中,我正在尝试创建一个将执行以下操作的异步函数:
开发一个名为“qAsync
”
的“函数声明”
Returns 一个包含两个键的对象(通过构造函数构建):
- ‘
doAsync
’: function: returnssetTimeout
可以使用async/await语法的函数 - ‘
exec
’:使用doAsync
函数在11.5秒后打印一些东西的函数 - ‘
desc
’:字符串:描述doAsync/exec()正在做什么以及如何使用它
但我不确定我的代码是否正确,因为我的测试文件出于某种原因无法识别我的执行程序。我在 learn.js 上的代码:
function qAsync(){
const doAsync = new doAsync(11500);
this.desc= "in order to wait 11.5 sec, call qAsync which calls asyncFun"
let exec= doAsync.exec(("hello after 11.5 sec"));
return (exec,doAsync);
}
let doAsync = async function (ms) {
return await new Promise(resolve => setTimeout(resolve, ms));
}
在 test.js:
function test4(){
let test = qAsync();
alert("first let us decribe the function:\n" + test.desc);
alert("now we will run exec.");
test.exec(); //doesn't work and calls on built in exec and not mine
}
我怎样才能让我的测试工作,你认为我应该改进我的 qAsync 吗?
回复感谢评论: learn.js:
function qAsync(){
this.doAsync = function (ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
this.desc = "in order to wait 11.5 sec, call exec which calls doAsync, and see the print in the console"
this.exec = async function () {
await this.doAsync(11500);
console.log("did you see this after 11.5 sec?");
}
}
test.js:
async function test4(){
let test = new qAsync();
alert("first let us describe the function:\n"+test.desc);
alert("now we will run exec.");
await test.exec();
}