/**
* 推荐基金页面JavaScript功能
*/
// 配置常量
const API_TIMEOUT = 5000; // API 请求超时时间(毫秒)
const MAX_RETRY_COUNT = 3; // 最大重试次数
const RETRY_DELAY = 1000; // 重试延迟(毫秒)
const BATCH_SIZE = 10; // 批量请求数量
const UPDATE_INTERVAL = 60000; // 自动更新间隔(毫秒)
const HIGHLIGHT_DURATION = 300; // 高亮效果持续时间
// 全局状态
let updateIntervalId = null;
let isLoading = false;
let fundDataCache = {}; // 缓存基金数据
let lastData = {}; // 上一次的数据,用于比较变化
let errorCount = 0;
const MAX_ERROR_COUNT = 5; // 连续错误次数上限
/**
* 格式化数字(保留两位小数)
* @param {number} num - 要格式化的数字
* @returns {string} 格式化后的字符串
*/
function formatNumber(num) {
if (typeof num !== 'number' || isNaN(num)) {
return '--';
}
return num.toFixed(2);
}
/**
* 显示加载状态
* @param {boolean} show - 是否显示
*/
function showLoading(show) {
const loadingElement = document.getElementById('loading-indicator');
if (!loadingElement && show) {
// 创建加载指示器
const div = document.createElement('div');
div.id = 'loading-indicator';
div.className = 'loading-overlay';
div.innerHTML = `
加载基金数据中...
`;
document.body.appendChild(div);
} else if (loadingElement) {
loadingElement.style.display = show ? 'flex' : 'none';
}
}
/**
* 清理JSONP请求的资源
* @param {string} scriptId - script标签ID
* @param {string} callbackName - 回调函数名
*/
function cleanupResources(scriptId, callbackName) {
// 清理script标签
const script = document.getElementById(scriptId);
if (script && script.parentNode) {
script.parentNode.removeChild(script);
}
// 清理回调函数
if (window[callbackName]) {
delete window[callbackName];
}
}
/**
* 处理API错误
* @param {string} errorMessage - 错误消息
*/
function handleApiError(errorMessage) {
errorCount++;
console.error(errorMessage);
// 如果连续错误次数过多,显示错误提示
if (errorCount >= MAX_ERROR_COUNT) {
updateErrorDisplay('获取基金数据时遇到问题,请稍后再试', true);
}
}
/**
* 获取单个基金数据(使用 JSONP 方式,支持预加载)
* @param {string} fundCode - 基金代码
* @param {number} retryCount - 当前重试次数
* @returns {Promise