Javascript - 试图围绕自定义事件(Javascript - Trying to wrap my head around custom events)
我正试图了解自定义事件。 我了解如何注册和触发自定义事件。 但是,似乎无法注册真正的自定义事件。 一切都必须追溯到DOM事件,如点击,上传,模糊等。或者我错了吗?
例如,假设我有一个数组。 我想注册一个在数组长度发生变化时触发的事件。 根据我的理解,我必须注册该事件,然后创建一个setInterval计时器,它根据先前存储的长度检查当前数组长度。 如果长度已经改变,那么我需要从setInterval内部触发我的自定义事件。
有没有办法为我的阵列注册一个事件,并在长度变化时自动激活它?
I'm trying to get my head around custom events. I understand how to register and trigger custom events. However, it seems like its not possible to register truly custom events. Everything has to trace back to a DOM event like click, onload, blur, etc. Or am I wrong?
For example, suppose I have an array. I want to register an event that fires when the length of the array changes. To my understanding, I would have to register the event and then create a setInterval timer that checks the current array length against the previously stored length. If the length has changed, I would then need to trigger my custom event from inside the setInterval.
Is there a way to register an event for my array and have it fire automatically when the length changes?
原文:https://stackoverflow.com/questions/568499
最满意答案
我遇到了性能API 。 您正在寻找的可能是
Performance.now()
,它将为您提供微秒级的精确度。
Performance.now()
方法返回一个以毫秒为单位的DOMHighResTimeStamp ,精确到千分之一毫秒,等于自PerformanceTiming.navigationStart属性和方法调用(源)以来的毫秒数。MDN提供的示例是:
var t0 = performance.now(); doSomething(); var t1 = performance.now(); console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.")
您可以使用多次测试特定短代码的性能的函数,您可以使用以下代码:
/** * Finds the performance for a given function * function fn the function to be executed * int n the amount of times to repeat * return array [time elapsed for n iterations, average execution frequency (executions per second)] */ function getPerf(fn, n) { var t0, t1; t0 = performance.now(); for (var i = 0; i < n; i++) { fn(i) } t1 = performance.now(); return [t1 - t0, repeat * 1000 / (t1 - t0)]; }
返回单次执行
fn(i)
花费的时间(以毫秒为单位),以及执行频率(每秒执行次数)。n
值越高,其精度越高,但测试所需的时间越长。 参数i
可以包含在要测试的函数中,其中包含函数的当前迭代。I came across the performance API. What you're looking for is probably
Performance.now()
, which will give you microsecond percision.The
Performance.now()
method returns a DOMHighResTimeStamp, measured in milliseconds, accurate to one thousandth of a millisecond equal to the number of milliseconds since the PerformanceTiming.navigationStart property and the call to the method (source).The example MDN provides is:
var t0 = performance.now(); doSomething(); var t1 = performance.now(); console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.")
A function you could use to test the performance of a specific short piece of code multiple times, you could use the following:
/** * Finds the performance for a given function * function fn the function to be executed * int n the amount of times to repeat * return array [time elapsed for n iterations, average execution frequency (executions per second)] */ function getPerf(fn, n) { var t0, t1; t0 = performance.now(); for (var i = 0; i < n; i++) { fn(i) } t1 = performance.now(); return [t1 - t0, repeat * 1000 / (t1 - t0)]; }
Which returns the amount of time a single execution of
fn(i)
takes in milliseconds, and the frequency of execution (executions per second). The higher then
value, the more precision this has, but the longer it takes to test. The argumenti
can be included in the function to be tested, which contains the current iteration of the function.
相关问答
更多-
函数的计算速度(Computation Speed of Functions)[2022-04-12]
你在这里谈论的那种决定通常被称为微优化 - 你通过微小的改变而不是重新思考程序的整体策略来优化代码。 通常,“微优化”的含义是“以不太明显的方式重写某些东西,意图挤出更多的性能”。 语言从函数中传递数据的明确方式是通过返回值,程序员习惯于看到原始类型,所以如果你要“脱离脚本”并使用这样的参数,那么应该有一个好的这样做的理由。 你是否绝对肯定这段代码执行得如此频繁以至于值得考虑重写会使得为了提高效率而难以阅读? 一个好的优化编译器可能无论如何都要内联这个函数调用,所以很可能没有多少成本。 为了确保实际上值得重 ... -
Javascript计算(Javascript Computation)[2021-09-28]
还有其他的事情我会改变,但最低限度的做法: var $row = $(this).closest('form'); http://jsfiddle.net/Uk5bN/1/ 当你在做$row = $(this).parent() ,你得到了li ,它是一个对等体而不是其他li的父对象,其中包含你想要$.find()的字段$.find() 。 form位于您尝试选择的所有input上方。 There's other things I would change, but minimally do: var $ ... -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
它有效地测试: (function(){xyz;}).toString() 返回可识别的javascript源代码: "(function(){xyz;})" 而不是某些实现可能返回的奇怪东西。 它使用.test将函数转换为字符串,然后验证内部令牌( xyz )在结果中是否可见。 Its effectively testing that: (function(){xyz;}).toString() returns recognizable javascript source code: "(function ...
-
您可以获得的最佳价值是GPS速度。 因此,请确保已将GPS设置为AccuracyBest并读取CLLocation speed属性。 你移动的速度越慢,就像所有GPS设备一样。 对于汽车典型速度,您可以获得0.1 km / h的精度。 The best value you can get is the GPS speed. So make sure you have set GPS to AccuracyBest and read the CLLocation speed attribute. The sl ...
-
检查这些链接Link1和Link2后 ,我意识到我的速度是正确的,我测试我的应用程序的汽车的速度表没有显示正确的值。 它们总是显示出真正的汽车速度。 因此,我的应用程序的价值优于汽车的速度表。 After checking these links Link1 and Link2, i realize that my speed is correct and the speedometer of the cars where i tested my app on do not show correct val ...
-
我遇到了性能API 。 您正在寻找的可能是Performance.now() ,它将为您提供微秒级的精确度。 Performance.now()方法返回一个以毫秒为单位的DOMHighResTimeStamp ,精确到千分之一毫秒,等于自PerformanceTiming.navigationStart属性和方法调用(源)以来的毫秒数。 MDN提供的示例是: var t0 = performance.now(); doSomething(); var t1 = performance.now(); cons ...
-
精确计算缩放互补误差函数,erfcx()(Accurate computation of scaled complementary error function, erfcx())[2022-07-07]
到目前为止我发现的erfcx()实现的最佳方法是基于以下文章: MM Shepherd和JG Laframboise,“Chebyshev逼近(1 + 2 x)exp(x 2 )erfc x在0≤x<∞。” 计算数学 ,第36卷,第153期,1981年1月,第249-253页(在线) 本文提出了巧妙的转换,它将缩放的互补误差函数映射到一个紧密有界的辅助函数,该函数适用于简单的多项式逼近。 为了提高性能,我已经尝试了各种变换,但所有这些都对精度产生了负面影响。 变换中常数K的选择(x-K)/(x + K)与核 ... -
事实上,PureDom测试有时被列为较慢,因为一些框架让我怀疑测量的准确性。 或者我猜测网站制作者的编程能力:P 特别是在看到这些数字后,我认为功能,易用性和社区支持比性能差异更重要。 使用你喜欢的东西,如果你偶然必须做一些非常依赖性能的东西,那就自己动手吧。 The fact that the PureDom tests are sometimes listed as being slower as some of the frameworks makes me doubt the accuracy of ...
-
有两种方法可以获得你想要的东西。 使用后台进程(Web worker)。 跟踪开头时间戳的时间,并显示now和then之间的差异,而不是每个时间间隔帧的增量。 Web工作者版 如果你可以支持web worker ,那么你可以使用它们来运行一个专用的后台进程,它支持你想要做的时间帧推送类型(在一个区间内逐帧增加一个定时器,没有时间戳差异,并保持它准确)。 以下是此网页上的示例: http://frenticb.com/tricks/simple-timer.phpA ...
相关文章
更多- JavaScript 事件
- JavaScript 事件
- [How to] Make custom search with Nutch(v 1.0)?(转)
- Custom SOLR Search Components - 2 Dev Tricks
- Hadoop0.20+ custom MultipleOutputFormat
- javascript 基本语法
- javascript 问题
- solr 使用自定义的 QueryParser
- Solr 使用自定义 Query Parser
- 《JavaScript模式》(JavaScript Patterns)扫描版[PDF]
最新问答
更多- sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
- 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
- AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
- Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
- 湖北京山哪里有修平板计算机的
- SimplePie问题(SimplePie Problem)
- 在不同的任务中,我们可以同时使用多少“上下文”?(How many 'context' we can use at a time simultaneously in different tasks?)
- HTML / Javascript:从子目录启用文件夹访问(HTML/Javascript: Enabling folder access from a subdirectory)
- 为什么我会收到链接错误?(Why do I get a linker error?)
- 如何正确定义析构函数(How to properly define destructor)
- 垂直切换菜单打开第3级父级。(Vertical toggle menu 3rd level parent stay opened. jQuery)
- 类型不匹配 - JavaScript(Type mismatch - JavaScript)
- 为什么当我将模型传递给我的.Net MVC 4控制器操作时,它坚持在部分更新中使用它?(Why is it that when I pass a Model to my .Net MVC 4 Controller Action it insists on using it in the Partial Update?)
- 在使用熊猫和statsmodels时拉取变量名称(Pulling variable names when using pandas and statsmodels)
- 如何开启mysql计划事件
- 检查数组的总和是否大于最大数,反之亦然javascript(checking if sum of array is greater than max number and vice versa javascript)
- 使用OpenGL ES绘制轮廓(Drawing Outline with OpenGL ES)
- java日历格式(java Calendar format)
- Python PANDAS:将pandas / numpy转换为dask数据框/数组(Python PANDAS: Converting from pandas/numpy to dask dataframe/array)
- 如何搜索附加在elasticsearch索引中的文档的内容(How to search a content of a document attached in elasticsearch index)
- LinQ to Entities:做相反的查询(LinQ to Entities: Doing the opposite query)
- 从ExtJs 4.1商店中删除记录时会触发哪些事件(Which events get fired when a record is removed from ExtJs 4.1 store)
- 运行javascript后如何截取网页截图[关闭](How to take screenshot of a webpage after running javascript [closed])
- 如何使用GlassFish打印完整的堆栈跟踪?(How can I print the full stack trace with GlassFish?)
- 如何获取某个exe应用程序的出站HTTP请求?(how to get the outbound HTTP request of a certain exe application?)
- 嗨,Android重叠背景片段和膨胀异常(Hi, Android overlapping background fragment and inflate exception)
- Assimp详细说明typedef(Assimp elaborated type refers to typedef)
- 初始化继承类中不同对象的列表(initialize list of different objects in inherited class)
- 使用jquery ajax在gridview行中保存星级评分(Save star rating in a gridview row using jquery ajax)
- Geoxml3 groundOverlay zIndex(Geoxml3 groundOverlay zIndex)