热门角色不仅是灵感来源,更是你的效率助手。通过精挑细选的角色提示词,你可以快速生成高质量内容、提升创作灵感,并找到最契合你需求的解决方案。让创作更轻松,让价值更直接!
我们根据不同用户需求,持续更新角色库,让你总能找到合适的灵感入口。
本提示词专为JavaScript开发场景设计,能够根据用户指定的循环条件和输出语言要求,生成符合最佳实践的while循环代码。通过结构化的工作流程,确保输出的代码具备技术准确性、逻辑清晰性和可维护性。特别适用于教学演示、代码审查和项目开发等场景,帮助开发者快速实现基于特定条件的循环逻辑,同时提供详细的代码解释和潜在优化建议。
/**
* 在 items 中按顺序查找第一个满足条件的元素。
* 使用 while (index < items.length && !foundTarget) 作为循环条件。
*
* @param {Array} items - 待搜索的数组
* @param {(item: any, index: number, array: Array) => boolean} isMatch - 判定函数,返回 true 表示命中
* @returns {{ foundTarget: boolean, matchedItem: any, index: number }}
* foundTarget: 是否找到目标
* matchedItem: 命中的元素(未找到时为 undefined)
* index: 命中元素的索引(未找到时为 -1)
*/
function findFirstMatch(items, isMatch) {
// 参数校验,避免运行时错误
if (!Array.isArray(items)) {
throw new TypeError('items 必须是数组');
}
if (typeof isMatch !== 'function') {
throw new TypeError('isMatch 必须是函数');
}
// 初始化循环相关变量
let index = 0; // 当前扫描位置
let foundTarget = false; // 是否已找到目标
let matchedItem; // 命中的元素
let resultIndex = -1; // 命中索引
// 核心循环:当索引未越界且尚未找到目标时继续
while (index < items.length && !foundTarget) {
const item = items[index];
// 判定当前元素是否符合条件
if (isMatch(item, index, items)) {
foundTarget = true;
matchedItem = item;
resultIndex = index;
// 虽然 while 条件已经依赖 foundTarget,但为了可读性与立即退出,这里显式 break
break;
}
// 推进指针,避免死循环
index += 1;
}
return { foundTarget, matchedItem, index: resultIndex };
}
/* 使用示例:
const items = [{ id: 1 }, { id: 2 }, { id: 3 }];
const targetId = 2;
const res = findFirstMatch(items, (it) => it.id === targetId);
// 期望:res => { foundTarget: true, matchedItem: { id: 2 }, index: 1 }
console.log(res);
*/
循环逻辑:
变量作用:
退出条件:
/**
* Polls until a readiness condition is met, a timeout occurs, or retries are exhausted.
* The loop condition follows:
* (now() - startTime) < timeoutMs && !isReady && retries < maxRetries
*
* @param {Function} checkReady - Async/sync function that returns a boolean indicating readiness.
* @param {Object} options
* @param {number} options.timeoutMs - Maximum total wait time in milliseconds.
* @param {number} options.maxRetries - Maximum number of failed checks allowed.
* @param {number} [options.intervalMs=100] - Delay between checks to avoid a busy loop.
* @returns {Promise<{ ready: boolean, retries: number, elapsedMs: number }>}
*/
async function waitUntilReady(checkReady, { timeoutMs, maxRetries, intervalMs = 100 }) {
if (typeof checkReady !== 'function') {
throw new TypeError('checkReady must be a function');
}
if (!Number.isFinite(timeoutMs) || timeoutMs < 0) {
throw new RangeError('timeoutMs must be a non-negative number');
}
if (!Number.isInteger(maxRetries) || maxRetries < 0) {
throw new RangeError('maxRetries must be a non-negative integer');
}
if (!Number.isFinite(intervalMs) || intervalMs < 0) {
throw new RangeError('intervalMs must be a non-negative number');
}
// Use a monotonic clock when available to avoid issues with system clock adjustments.
const now = (typeof performance !== 'undefined' && typeof performance.now === 'function')
? () => performance.now()
: () => Date.now();
const startTime = now(); // tracks when polling started
let retries = 0; // number of unsuccessful attempts
let isReady = false; // readiness state returned by checkReady()
// Sleep helper to avoid a tight loop.
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
while ((now() - startTime) < timeoutMs && !isReady && retries < maxRetries) {
try {
// Attempt to determine readiness.
// Supports both sync and async checkReady implementations.
isReady = await checkReady();
} catch (err) {
// Treat exceptions as a failed check; consider logging if needed.
isReady = false;
}
if (isReady) break; // Exit early if the condition is met.
// Count this as a failed attempt only when not ready.
retries += 1;
// Compute remaining time; do not oversleep past the timeout.
const elapsed = now() - startTime;
const remaining = Math.max(0, timeoutMs - elapsed);
// Break if no time remains or retries will stop the next loop.
if (remaining === 0 || retries >= maxRetries) break;
// Sleep for a bounded interval to prevent CPU spin.
await sleep(Math.min(intervalMs, remaining));
}
return {
ready: isReady,
retries,
elapsedMs: now() - startTime,
};
}
/* Example usage:
(async () => {
const result = await waitUntilReady(async () => {
// Replace with your real readiness check (e.g., probing an API or checking a flag).
return Math.random() > 0.8;
}, { timeoutMs: 3000, maxRetries: 10, intervalMs: 150 });
console.log(result);
})();
*/
'use strict';
/**
* 指定された条件(queuePointer < queue.length && processedCount < batchLimit)で
* キューを先頭から順に処理します(同期処理版)。
*
* 注意:
* - processedCount は「試行した件数」を表します(成功/失敗を問わず進みます)。
* 成功件数のみをカウントしたい場合は、try ブロック内で加算する実装に変更してください。
*
* @param {any[]} queue - 処理対象の配列。ループ中に長さを変更しないことを推奨します。
* @param {number} batchLimit - 1バッチで試行する最大件数(0以上の整数)。
* @param {number} startPointer - 開始インデックス(0以上の整数、範囲外は自動で丸め込み)。
* @param {(item:any, index:number)=>void} handle - 各要素を処理する関数(同期)。
* @returns {{ nextPointer: number, attempted: number, errors: Error[] }}
*/
function processBatch(queue, batchLimit, startPointer = 0, handle = (item) => void item) {
// 入力バリデーション(早期失敗で安全性を確保)
if (!Array.isArray(queue)) {
throw new TypeError('queue は配列である必要があります。');
}
if (!Number.isInteger(batchLimit) || batchLimit < 0) {
throw new TypeError('batchLimit は 0 以上の整数である必要があります。');
}
if (!Number.isInteger(startPointer) || startPointer < 0) {
throw new TypeError('startPointer は 0 以上の整数である必要があります。');
}
if (typeof handle !== 'function') {
throw new TypeError('handle は関数である必要があります。');
}
// 反復用ポインタ(開始位置は配列長を超えないように丸める)
let queuePointer = Math.min(startPointer, queue.length);
// 今回のバッチで試行した件数
let processedCount = 0;
// エラーを収集(必要に応じてロギング/監視システムへ送信)
const errors = [];
// ループ条件:
// - まだ未処理の要素が残っている(queuePointer < queue.length)
// - かつ、今回のバッチの上限に達していない(processedCount < batchLimit)
while (queuePointer < queue.length && processedCount < batchLimit) {
const item = queue[queuePointer];
try {
// 各要素の処理(同期)
// ここで例外が発生しても finally でポインタとカウントを進めるため無限ループになりません。
handle(item, queuePointer);
} catch (err) {
// 個別要素の失敗は収集して継続(バッチ全体は止めない)
errors.push(err instanceof Error ? err : new Error(String(err)));
} finally {
// 次の要素へ進む(試行件数も加算)
queuePointer += 1;
processedCount += 1;
}
}
return {
nextPointer: queuePointer, // 次回再開時の開始位置
attempted: processedCount, // 今回試行した件数(成功/失敗を含む)
errors, // 個別エラー一覧
};
}
// 使用例:
// const result = processBatch(myQueue, 100, 0, (item, index) => {
// // TODO: 各要素の処理ロジック
// });
循环逻辑:
变量作用:
退出条件:
用一句话概括:把“写 while 循环”这件高频又易出错的小事,交给专业提示词,省时、省心、更稳。
基于交互状态轻松生成列表遍历、滚动检测、输入校验等循环;快速插入注释与退出条件;减少调试时间,提升提测效率。
为队列消费、任务重试、定时轮询等场景生成稳健循环;内置超时与中止建议;降低线上宕机与资源占用风险。
一键产出教学示例与讲解要点;展示变量作用、边界处理与常见误区;用于课堂演示、练习讲评与教材编写。
将模板生成的提示词复制粘贴到您常用的 Chat 应用(如 ChatGPT、Claude 等),即可直接对话使用,无需额外开发。适合个人快速体验和轻量使用场景。
把提示词模板转化为 API,您的程序可任意修改模板参数,通过接口直接调用,轻松实现自动化与批量处理。适合开发者集成与业务系统嵌入。
在 MCP client 中配置对应的 server 地址,让您的 AI 应用自动调用提示词模板。适合高级用户和团队协作,让提示词在不同 AI 工具间无缝衔接。
半价获取高级提示词-优惠即将到期