diff --git a/zh-cn/application-dev/reference/apis/js-apis-taskpool.md b/zh-cn/application-dev/reference/apis/js-apis-taskpool.md index 7f662943e812c30633716158082c73c2c28fa23a..ee10746563fe247ab24658a01fc503d9841ba538 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-taskpool.md +++ b/zh-cn/application-dev/reference/apis/js-apis-taskpool.md @@ -64,6 +64,7 @@ function func(args) { console.log("func: " + args); return args; } + let task = new taskpool.Task(func, "this is my first Task"); ``` @@ -116,7 +117,12 @@ function func(args) { return args; } -let value = taskpool.execute(func, 100); +async function taskpoolTest() { + let value = await taskpool.execute(func, 100); + console.log("taskpool result: " + value); +} + +taskpoolTest(); ``` ## taskpool.execute @@ -158,8 +164,14 @@ function func(args) { console.log("func: " + args); return args; } -let task = new taskpool.Task(func, "this is my first Task"); -let value = taskpool.execute(task); + +async function taskpoolTest() { + let task = new taskpool.Task(func, 100); + let value = await taskpool.execute(task); + console.log("taskpool result: " + value); +} + +taskpoolTest(); ``` ## taskpool.cancel @@ -193,9 +205,14 @@ function func(args) { console.log("func: " + args); return args; } -let task = new taskpool.Task(func, "this is first Task"); -let value = taskpool.execute(task); -taskpool.cancel(task); + +async function taskpoolTest() { + let task = new taskpool.Task(func, 100); + let value = await taskpool.execute(task); + taskpool.cancel(task); +} + +taskpoolTest(); ``` ## 其他说明 @@ -214,10 +231,18 @@ function func(args) { return args; } -let task = new taskpool.Task(func, "create task, then execute"); -let val1 = taskpool.execute(task); +async function taskpoolTest() { + // taskpool.execute(task) + let task = new taskpool.Task(func, "create task, then execute"); + let val1 = await taskpool.execute(task); + console.log("taskpool.execute(task) result: " + val1); -let val2 = taskpool.execute(func, "execute task by func"); + // taskpool.execute(function) + let val2 = await taskpool.execute(func, "execute task by func"); + console.log("taskpool.execute(function) result: " + val2); +} + +taskpoolTest(); ``` ```js @@ -226,7 +251,7 @@ let val2 = taskpool.execute(func, "execute task by func"); // b.ts export var c = 2000; -// a.ts +// a.ts(与b.ts同目录) import { c } from './b' function test(a) { @@ -236,8 +261,16 @@ function test(a) { return a; } -let task = new taskpool.Task(test, "create task, then execute"); -let val1 = taskpool.execute(task); +async function taskpoolTest() { + // taskpool.execute(task) + let task = new taskpool.Task(test, "create task, then execute"); + let val1 = await taskpool.execute(task); + console.log("taskpool.execute(task) result: " + val1); + + // taskpool.execute(function) + let val2 = await taskpool.execute(test, "execute task by func"); + console.log("taskpool.execute(function) result: " + val2); +} -let val2 = taskpool.execute(test, "execute task by func"); +taskpoolTest(); ``` \ No newline at end of file diff --git a/zh-cn/application-dev/reference/apis/js-apis-worker.md b/zh-cn/application-dev/reference/apis/js-apis-worker.md index 9e98a5244869cf5fd48b55ee677f6b6a860e9949..6496d1a3507acc23399a3f554608b27c6819e59f 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-worker.md +++ b/zh-cn/application-dev/reference/apis/js-apis-worker.md @@ -81,19 +81,19 @@ ThreadWorker构造函数。 import worker from '@ohos.worker'; // worker线程创建 -// FA模型-目录同级 +// FA模型-目录同级(entry模块下,workers目录与pages目录同级) const workerFAModel01 = new worker.ThreadWorker("workers/worker.js", {name:"first worker in FA model"}); -// FA模型-目录不同级(以workers目录放置pages目录前一级为例) +// FA模型-目录不同级(entry模块下,workers目录与pages目录的父目录同级) const workerFAModel02 = new worker.ThreadWorker("../workers/worker.js"); -// Stage模型-目录同级 +// Stage模型-目录同级(entry模块下,workers目录与pages目录同级) const workerStageModel01 = new worker.ThreadWorker('entry/ets/workers/worker.ts', {name:"first worker in Stage model"}); -// Stage模型-目录不同级(以workers目录放置pages目录后一级为例) +// Stage模型-目录不同级(entry模块下,workers目录是pages目录的子目录) const workerStageModel02 = new worker.ThreadWorker('entry/ets/pages/workers/worker.ts'); // 理解Stage模型scriptURL的"entry/ets/workers/worker.ts": -// entry: 为module.json5文件中module的name属性对应的值; -// ets: 表明当前使用的语言。 +// entry: 为module.json5文件中module的name属性对应的值,ets: 表明当前使用的语言。 +// scriptURL与worker文件所在的workers目录层级有关,与new worker所在文件无关。 ``` 同时,需在工程的模块级build-profile.json5文件的buildOption属性中添加配置信息,主要分为下面两种情况: @@ -585,6 +585,15 @@ dispatchEvent(event: Event): boolean ```js const workerInstance = new worker.ThreadWorker("workers/worker.js"); + +workerInstance.dispatchEvent({type:"eventType", timeStamp:0}); //timeStamp暂未支持。 +``` + +分发事件(dispatchEvent)可与监听接口(on、once、addEventListener)搭配使用,示例如下: + +```js +const workerInstance = new worker.ThreadWorker("workers/worker.js"); + //用法一: workerInstance.on("alert_on", (e)=>{ console.log("alert listener callback"); @@ -751,6 +760,15 @@ dispatchEvent(event: Event): boolean ```js const workerInstance = new worker.ThreadWorker("workers/worker.js"); + +workerInstance.dispatchEvent({type:"eventType", timeStamp:0}); //timeStamp暂未支持。 +``` + +分发事件(dispatchEvent)可与监听接口(on、once、addEventListener)搭配使用,示例如下: + +```js +const workerInstance = new worker.ThreadWorker("workers/worker.js"); + //用法一: workerInstance.on("alert_on", (e)=>{ console.log("alert listener callback"); @@ -1593,6 +1611,14 @@ dispatchEvent(event: Event): boolean ```js const workerInstance = new worker.Worker("workers/worker.js"); +workerInstance.dispatchEvent({type:"eventType", timeStamp:0}); //timeStamp暂未支持。 +``` + +分发事件(dispatchEvent)可与监听接口(on、once、addEventListener)搭配使用,示例如下: + +```js +const workerInstance = new worker.Worker("workers/worker.js"); + //用法一: workerInstance.on("alert_on", (e)=>{ console.log("alert listener callback");