xxx.toFixed is not a function,js toFixed 报错

================================

©Copyright 蕃薯耀 2022-03-30

​​蕃薯耀的博客_优快云博客

一、问题描述

count.toFixed(2);

count.toFixed(2);报错:Uncaught TypeError: count.toFixed is not a function

二、解决方案

报错的原因是因为count不是Number类型,需要先转成Number

Number(count).toFixed(2);

或者:
parseInt(count).toFixed(2)
parseFloat(count).toFixed(2)

三、toFixed用法

number.toFixed(x)

把数字转换为字符串,结果的小数点后有指定位数的数字。


x参数:

必需。规定小数的位数,是 0 ~ 20 之间的值,包括 0 和 20,有些实现可以支持更大的数值范围。如果省略了该参数,将用 0 代替。

 (时间宝贵,分享不易,捐赠回馈,^_^)

================================

©Copyright 蕃薯耀 2022-03-30

​​蕃薯耀的博客_优快云博客

这是我写的: // 简单的 WebRTC + video 打点脚本 (() => { const OrigPC = window.RTCPeerConnection || window.webkitRTCPeerConnection; if (!OrigPC) { console.warn('No RTCPeerConnection on this page'); return; } console.log('[MONKEY] Patching RTCPeerConnection...'); window.RTCPeerConnection = function (...args) { const pc = new OrigPC(...args); pc.__id = Math.random().toString(36).slice(2, 8); const origCreateDC = pc.createDataChannel.bind(pc); pc.createDataChannel = function (label, options) { const t0 = performance.now(); const dc = origCreateDC(label, options); dc.__pcId = pc.__id; dc.__label = label; dc.__tCreate = t0; console.log(`[DC] createDataChannel pc=${pc.__id} label=${label} at ${t0.toFixed(1)}ms`); dc.addEventListener('open', () => { console.log(`[DC] open pc=${dc.__pcId} label=${dc.__label} +${(performance.now() - dc.__tCreate).toFixed(1)}ms`); }); // 你要的话也可以监听 message,看看是不是有 seek 请求/回应 dc.addEventListener('message', (ev) => { // console.log(`[DC] msg label=${dc.__label}`, ev.data); }); return dc; }; // 如果是对端创建的 datachannel(ondatachannel) pc.addEventListener('datachannel', (ev) => { const dc = ev.channel; const t0 = performance.now(); dc.__pcId = pc.__id; dc.__label = dc.label; dc.__tCreate = t0; console.log(`[DC] ondatachannel pc=${pc.__id} label=${dc.label} at ${t0.toFixed(1)}ms`); dc.addEventListener('open', () => { console.log(`[DC] (remote) open pc=${pc.__pcId} label=${dc.__label} +${(performance.now() - dc.__tCreate).toFixed(1)}ms`); }); }); return pc; }; let tSeekEnd = null; // 时间轴拖动作结束为监听起始 document.querySelectorAll('[class*="TimelineBlock__TimelineCover"][class*="dvDOqB"]').forEach((el) => { el.addEventListener('pointerup', () => { tSeekEnd = performance.now(); console.log( '[UX] seek end at', tSeekEnd.toFixed(1), 'ms' ); }); }); // 监听页面上的所有video,看什么时候真正开始播放 function hookVideo(v) { if (!v || v.__hooked) return; v.__hooked = true; v.addEventListener('loadeddata', () => { console.log( '[VIDEO] loadeddata at', performance.now().toFixed(1), 'ms', 'currentTime=', v.currentTime.toFixed(3) ) }) const now = performance.now(); v.addEventListener('playing', () => { console.log( '[VIDEO] playing at', now.toFixed(1), 'ms', 'size=', v.videoWidth, 'x', v.videoHeight ); }); if (tSeekEnd != null) { console.log( '[METRIC] seekEnd → playing =', (now - tSeekEnd).toFixed(1), 'ms' ); } } // 钩当前已有的视频 document.querySelectorAll('video').forEach(hookVideo); // 监听后续 DOM 变化,自动钩新的 video const mo = new MutationObserver(muts => { for (const m of muts) { for (const n of m.addedNodes) { if (n.tagName === 'VIDEO') { hookVideo(n); } else if (n.querySelectorAll) { n.querySelectorAll('video').forEach(hookVideo); } } } }); mo.observe(document.documentElement, { childList: true, subtree: true }); console.log('[MONKEY] RTCPeerConnection & hooks installed'); })(); 能不能换成xxx.xxx
最新发布
11-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值