首页 > 其他分享 >js常见问题

js常见问题

时间:2024-04-23 13:58:49浏览次数:18  
标签:chromeVersion 常见问题 const setAttribute hiddenCanvas js videoDom

Chrome 88后更新了更为激进的省电策略[1],导致后台非活动页面中setInterval、setTimeout回调的执行间隔拉长到1分钟以上。

这里有一种很巧妙的方法可以强制标签页不进入非活动状态(利用了播放视频时标签页不会休眠的机制),而且不需要改动业务代码,只需要将下面的代码放到页面JS脚本中执行即可:

const chromeVersion = /Chrome\/([0-9.]+)/
  .exec(window?.navigator?.userAgent)?.[1]
  ?.split('.')[0];

if (chromeVersion && parseInt(chromeVersion, 10) >= 88 && !isMobile()) {
  const videoDom = document.createElement('video');
  const hiddenCanvas = document.createElement('canvas');

  videoDom.setAttribute('style', 'display:none');
  videoDom.setAttribute('muted', '');
  videoDom.muted = true;
  videoDom.setAttribute('autoplay', '');
  videoDom.autoplay = true;
  videoDom.setAttribute('playsinline', '');
  hiddenCanvas.setAttribute('style', 'display:none');
  hiddenCanvas.setAttribute('width', '1');
  hiddenCanvas.setAttribute('height', '1');
  hiddenCanvas.getContext('2d')?.fillRect(0, 0, 1, 1);
  videoDom.srcObject = hiddenCanvas?.captureStream();
}

参考:https://www.zhihu.com/question/263914822

标签:chromeVersion,常见问题,const,setAttribute,hiddenCanvas,js,videoDom
From: https://www.cnblogs.com/heysong/p/18152702

相关文章

  • 使用js语文json字符串通过get方式传值,然后用request接收
    在JavaScript中,如果你有一个JSON字符串,并且你想通过GET请求或者其他HTTP请求来传递这个字符串,你可以使用encodeURIComponent函数来确保字符串能够安全地通过URL传输。//假设我们有一个JSON对象constjsonObject={name:"John",age:30,city:"NewYork"};//将......
  • js逆向实战之一品威客signature参数解密
    url:https://www.epwk.com/login.html分析过程输入用户名和密码,看触发的流量包。signature参数明显是被加密过的,接下来就是去寻找加密的过程。关键词搜索signature。有两处,第二处是个固定值不需要看,关注点在第一处。点进去看对应的代码,并打断点,重新登录,触发该断点。......
  • js递归实现深拷贝
    functiongetType(data){returnObject.prototype.toString.call(data).replace(']','').substring(8)}function_clone(data,res){consttype=getType(data)if(type==='Object'){Object.keys(data).map(i=>{......
  • js AddDays ,AddYears
    //日期类型变量格式化,默认格式"xxxx-xx-xx"Date.prototype.Format=function(fmt){fmt=fmt||"yyyy-MM-dd";varo={"M+":this.getMonth()+1,//月份"d+":this.getDate(),//日"h+":......
  • three.js使用Instanced Draw+Frustum Cull+LOD来渲染大场景(开源)
    大家好,本文使用three.js实现了渲染大场景,在移动端也有较好的性能,并给出了代码,分析了关键点,感谢大家~关键词:three.js、InstancedDraw、大场景、LOD、FrustumCull、优化、Web3D、WebGL、开源代码:Github我正在承接Web3D数字孪生项目,具体介绍可看承接各种Web3D业务加QQ群交流:106......
  • fastjson导致的程序崩溃:A fatal error has been detected by the Java Runtime Enviro
    ##AfatalerrorhasbeendetectedbytheJavaRuntimeEnvironment:##EXCEPTION_ACCESS_VIOLATION(0xc0000005)atpc=0x000001da4d3ab6b3,pid=15996,tid=0x0000000000006478##JREversion:Java(TM)SERuntimeEnvironment(8.0_361)(build1.8.0_361-b09)......
  • gojs2.1去水印
    go.jsgo-debug.jsgo-module.jsES6模块go-debug-module.js这四个文件,具体看你使用哪一个搜索String.fromCharCode找到下图位置,这是我的格式化之后的样子 然后在 returnf 之前添加代码if(f.indexOf('GoJS2.1evaluation')>-1||f.indexOf('(c)1998-......
  • vis.js摄像机位置
    代码案例<!DOCTYPEhtml><html><head><title>Graph3Ddemo</title><style>body{font:10ptarial;}td{font:10ptarial;}</style><s......
  • vis.js绘制3d图形
    案例1代码案例<!DOCTYPEhtml><html><head><title>Graph3Ddemo</title><scripttype="text/javascript"src="https://unpkg.com/vis-graph3d@latest/dist/vis-graph3d.min.js"><......
  • vis.js分组多图折线图
    代码案例<!doctypehtml><html><head><title>Timeline</title><scripttype="text/javascript"src="https://unpkg.com/vis-timeline@latest/standalone/umd/vis-timeline-graph2d.min.js"></script>......