Effect Queues

本文介绍如何使用队列来管理JavaScript中的多个效果,确保效果按指定顺序执行而非并行。通过Effect.Queue和Effect.ScopedQueue,可以对不同元素的效果进行排队,并设置队列长度限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[b]What is a Queue[/b]
Let’s examine how events in a queue occur: For a programmer a queue is an Array with events that are stacked on each other to be handled one by one. For example, a user hovers over a button with his cursor and then after half a second leaves the button area again. Two events have taken place, the mouseover and the mouseout. If you would handle the mouseover and the mouseout event takes place, it could disturb your current process. However you can’t just ignore the event, because then you will be stuck in the status that is established by the actions you have performed for mouseover. In this situation you could add the event to a queue and every time you’re done with processing an event, just check the queue for unprocessed events (and act accordingly).
[b]How do Effects work and why do you need a Queue[/b]
The same happens when you are dealing with Effects and you face the situation where two effects can be called, both of which manipulate the same DOM object(s). script.aculo.us comes to the rescue with several techniques to manage your effects. To explain this further, we will show you how things work by example.

[b]什么是queue? 为了解决对同一个元素触发多个处理效果时需要解决效果发生的先后顺序问题。通过Queue队列的形式将并行的效果按我们的先后顺序串行化。[/b]

Effect.Queue
The previous examples gave you an idea of the situations you could be facing without Queues. This must give you some motivation to find how Effect.Queue could improve your live and help you to manage your Effects.

Effect.Queue is an improved array of Effects (called Enumerable) that is global for the entire page. It gets created by extracting the global queue from Effect.Queues (we will discuss this later). So all effects you want to queue will be added to the ‘global’ queue. You might be wondering how to add an effect to the queue? Well it’s quite easy.

[b]Effect.Queue是一个改进的Effects数组。此对象作用范围为页面全局可访问。[/b]

new Effect.SlideDown('test1');
new Effect.SlideUp('test1', { queue: 'end'});


So you see I have added an array containing an entry “queue:” with the value “end”. This means that the Effect will be inserted at the end of the “global” Queue. All effects are always added to the queue, however without a position like ‘end’ they are always executed parallel to the other effects.

[b]如果没有queue:end,效果将并行的执行。[/b]

[b]Remember that JavaScript is not multi-threaded, so blocks of code are executed together when they get parsed. This causes the effects to start in parallel if no queue is defined.[/b]

This works nicely, doesn’t it? But what happens when you queue all kinds of effects in different places? You could have lots of effects that you would like to queue but they don’t all relate to each other. This is where [b]Effect.ScopedQueue [/b]comes in.

[b]Basic Effect.ScopedQueue[/b]

As explained before, the [b]default Queue is ‘global’[/b]. All effects are placed in the ‘global’ Queue unless you define a different scope. A scope is nothing more than grouping a set of Effects together in their own Queue. To do this, you need to pass an array instead of a string to the “queue:” option (so you have an array inside the outer array). This can only be done by using a different syntax to define the Queue position.


new Effect.SlideUp('menu', {queue: {position:'end', scope: 'menuxscope'} });
new Effect.SlideUp('bannerbig', {queue: {position:'end', scope: 'bannerscope'} });
new Effect.SlideDown('menu', {queue: {position: 'end', scope: 'menuxscope'} });


[b]Limit 限制队列长度[/b]
Sometimes too many effects get queued, for example when a user presses a button twice, or repeatedly enters and leaves a button hover area (and each time two effects are queued). This can cause a lot of distracting activity. To prevent this, we can add a “limit: n” option to the queue so no more than n effects are added to that ScopedQueue.

new Effect.SlideDown('menu', {queue: {position:'end', scope: 'menuxscope', limit:2} }); // #1
new Effect.Highlight('menu', {queue: {position:'end', scope: 'menuxscope', limit:2} }); // #2
new Effect.SlideUp('menu', {queue: {position: 'end', scope: 'menuxscope', limit:2} }); // #3


[b]#1 and #2 will be added to the queue but #3 will not.[/b]

[b]Effect.Queues[/b]
So what is Effect.Queues and why do we need it? Well, for each scope an instance of Effect.Scoped Queue? is created. That instance is stored in Effect.Queues. You can access it by using Effect.Queues.get(‘global’) which is the same as Effect.Queue because the default ‘global’ queue is saved into Effect.Queue by script.aculo.us. However when you need to access a scope other than ‘global, you need to fetch it using Effect.Queues.get(‘myscope’) before you can manipulate it.

我们可以从Effect.Queues得到我们创建的Queue。因为它们都保存在Effect.Queues中
了!
var queue = Effect.Queues.get('myscope');


ScopedQueue is just an Enumerable object. You can retrieve the effects one by one and manipulate them. Let’s look at how we can ‘empty a queue’.

var queue = Effect.Queues.get('myscope');
queue.each(function(e) { e.cancel() });


Or maybe you want to change the interval between the effects in a queue:
Effect.Queues.get('myscope').interval = 100;


更多精彩细节?
请看[url]http://wiki.script.aculo.us/scriptaculous/show/EffectQueues[/url]
CCEffect %{ techniques: - passes: - vert: vs frag: fs properties: &props mainTexture: { value: 'white' } // 修改为mainTexture outlineColor: { value: [1,0,0,1], editor: { type: 'color' } } outlineWidth: { value: 0.02, editor: { type: 'slider', min: 0, max: 0.1 } } }% CCProgram vs %{ precision highp float; #include <cc-global> #include <cc-local> in vec3 a_position; in vec2 a_texCoord; in vec4 a_color; out vec4 v_color; out vec2 v_uv0; void main() { vec4 pos = vec4(a_position, 1.0); pos = cc_matWorld * pos; gl_Position = cc_matProj * cc_matView * pos; v_uv0 = a_texCoord; v_color = a_color; } }% CCProgram fs %{ precision highp float; uniform sampler2D mainTexture; // 修改为mainTexture uniform Constant { vec4 outlineColor; float outlineWidth; }; in vec4 v_color; in vec2 v_uv0; void main() { vec4 texColor = texture2D(mainTexture, v_uv0); // 修改为mainTexture float alpha = texColor.a * v_color.a; // 边缘检测 float maxAlpha = 0.0; for(int x = -1; x <= 1; ++x) { for(int y = -1; y <= 1; ++y) { vec2 offset = vec2(x,y) * outlineWidth; maxAlpha = max(maxAlpha, texture2D(mainTexture, v_uv0 + offset).a); // 修改为mainTexture } } // 颜色混合 vec4 finalColor = mix(outlineColor, v_color * texColor, alpha); finalColor.a = max(outlineColor.a * (maxAlpha - alpha), alpha); gl_FragColor = finalColor; } }% 以上在cocoscreator3.8.2中创建的spine描边.effect报错 1. [Assets] ../bundle/gui/shaders/spine-outline.effect - syntax: Error EFX1001: CCEffect parser failed: YAMLException: bad indentation of a mapping entry (7:43) 4 | ... t: vs 5 | ... g: fs 6 | ... perties: &props 7 | ... ainTexture: { value: 'white' } // 修改为mainTexture -----------------------------------------^ 8 | ... utlineColor: { value: [1,0,0,1], editor: { type: 'color' } } 9 | ... utlineWidth: { value: 0.02, editor: { type: 'slider', min: 0, m ... [Assets] ../bundle/gui/shaders/spine-outline.effect - syntax: Error EFX1001: CCEffect parser failed: YAMLException: bad indentation of a mapping entry (7:43) -----------------------------------------^ -----------------------------------------^ at Object.encode (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules\@base\electron-worker\node_modules\v-stacks\index.js:14:17) at Logger._logHandler (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules\@base\electron-worker\static\script.ccc:1:531) at Logger.record (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules\@base\electron-logger\lib\renderer.ccc:1:458) at Object.error (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules\@base\electron-logger\lib\renderer.ccc:1:1414) at Object.error (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\builtin\asset-db\dist\worker\console.ccc:1:1429) at EffectImporter.import (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\modules\engine-extensions\extensions\engine-extends\dist\importer\importers\effect.ccc:1:3086) at runMicrotasks (<anonymous>) at runNextTicks (internal/process/task_queues.js:58:5) at processImmediate (internal/timers.js:434:9) 2. [Window] The "path" argument must be of type string or an instance of Buffer or URL. Received undefined TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received undefined at Object.openSync (fs.js:468:10) at Object.func [as openSync] (electron/js2c/asar_bundle.js:5:1846) at readFileSync (fs.js:377:35) at e.readFileSync (electron/js2c/asar_bundle.js:5:8872) at Object.refresh (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\resources\3d\engine\editor\inspector\assets\effect.js:313:39) at Object.<anonymous> (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\resources\3d\engine\editor\inspector\assets\effect.js:415:26) at HTMLElement.update (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules\@editor\creator-ui-kit\dist\renderer.ccc:2:1642565) at C:\ProgramData\cocos\editors\Creator\3.8.2\resources\resources\3d\engine\editor\inspector\contributions\asset.js:358:43 at Array.map (<anonymous>) at Object.update (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\resources\3d\engine\editor\inspector\contributions\asset.js:357:50)
05-28
### FreeRTOS 队列的使用方法 FreeRTOS 提供了一种灵活且高效的机制来处理任务间通信,其中队列是一个重要的组成部分。通过创建和管理队列,可以实现不同优先级的任务之间的数据传递。 #### 创建队列 要创建一个队列,需调用 `xQueueCreate` 函数并指定队列长度以及每个项的大小: ```c QueueHandle_t xQueue; const UBaseType_t uxQueueLength = 10; // 定义队列的最大长度为10个元素 const UBaseType_t uxItemSize = sizeof( uint32_t ); // 设置每条消息的数据类型大小 // 创建具有特定长度和项目尺寸的新队列 xQueue = xQueueCreate(uxQueueLength, uxItemSize); if (xQueue == NULL) { // 如果返回NULL,则表示内存不足无法创建队列 } ``` #### 向队列发送数据 向队列中发送数据有两种方式:阻塞模式和非阻塞模式。对于实时应用来说,通常推荐采用带超时参数的方法以防止无限期挂起。 - **阻塞模式** 当队列已满时,在规定时间内等待空间释放再尝试写入新数据;如果时间到达仍未成功则放弃此次操作。 ```c uint32_t ulVarToSend = 99UL; // 尝试在portMAX_DELAY时间内将ulVarToSend放入到名为xQueue的队列里 if (xQueueSend(xQueue, &ulVarToSend, portMAX_DELAY) != pdPASS){ // 发送失败后的错误处理逻辑 } ``` - **非阻塞模式** 立即检查是否有可用位置用于存储新的数据项,如果有就立刻执行插入动作;如果没有则直接跳过而不做任何改变。 ```c if (xQueueSendToBackFromISR(xQueue, &ulVarToSend, NULL) != pdTRUE){ // ISR上下文中发送失败后的错误处理逻辑 } ``` #### 接收来自队列的消息 同样存在两种接收策略——阻塞性读取与即时性获取。前者会在没有接收到有效载荷前一直保持当前线程处于休眠状态直到满足条件或者达到设定时限为止;后者则是快速判断是否存在可读取的内容,一旦发现即刻完成相应工作流程。 - **阻塞模式** 设置最大允许延时期限去监听目标队列中的最新更新情况,超过该期限未获得预期结果会触发异常响应路径。 ```c uint32_t ulReceivedValue; // 等待最多10个滴答周期从队列中取出一项 if (xQueueReceive(xQueue, &ulReceivedValue, 10 / portTICK_PERIOD_MS) == pdPASS) { // 成功接收到数据后的处理过程 } else { // 超时或其他原因导致未能正常取得值的情况下的应对措施 } ``` - **非阻塞模式** 迅速检测目的地址是否含有可供提取的信息单元,若有便马上实施转移作业;反之亦然。 ```c if (xQueuePeek(xQueue, &ulReceivedValue, 0) == pdPASS) { // 查看但不移除最前面的一个元素 } if (xQueueReceive(xQueue, &ulReceivedValue, 0) == pdPASS) { // 获取并删除最前端的对象实例 } ``` 为了确保代码能够正确编译运行,需要注意头文件包含顺序的问题[^2]。此外,还可以自定义宏定义以便更好地跟踪调试过程中涉及的具体行为表现[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值