简体   繁体   English

如何在 setTimeout 中测试用户指定的时间

[英]How to test for user-specified time in setTimeout

Right now I have a jQuery plugin that adds some extra customization (around how to handle the return data in certain contexts) and polling a server for information and changes.现在我有一个 jQuery 插件,它添加了一些额外的自定义(关于如何在某些上下文中处理返回数据)并轮询服务器以获取信息和更改。 One of the required features is to allow the user set the polling interval.所需功能之一是允许用户设置轮询间隔。 setTimeout (paired with recursion) is the function that is used for creating the constant poll; setTimeout (与递归配对)是用于创建常量轮询的 function; this is also a requirement.这也是一个要求。

This is where I am stumped: along with the required functionality noted above, I also have to test if the user-specified value was actually used in the setTimeout.这就是我被难住的地方:除了上面提到的所需功能外,我还必须测试用户指定的值是否实际用于 setTimeout。 How would I go about doing this from a QUnit perspective?从 QUnit 的角度来看,我将如何 go 这样做?

I have already pondered straight timing the function with Date.getTime(), but the way things are currently set up this isn't an option.我已经考虑过使用 Date.getTime() 直接为 function 计时,但目前的设置方式不是一个选项。

Consider refactoring the code in a way that let's you override the method starting the timeout within your test, allowing you to verify that the correct value is passed, without actually starting the timeout.考虑以某种方式重构代码,让您在测试中覆盖启动超时的方法,允许您验证是否传递了正确的值,而无需实际启动超时。

If you need to actually call setTimeout, consider using sinon.js to mock setTimeout itself and control what it does instead.如果您需要实际调用 setTimeout,请考虑使用sinon.js来模拟 setTimeout 本身并控制它的作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将用户指定的值获取到javascript文件中 - get user-specified values into javascript file Chrome 扩展程序如何将多个文件保存到用户指定的目录? - How can a Chrome extension save many files to a user-specified directory? 如何编写可以生成用户指定数量的 css 方块的脚本 - How to write a script that can produce a user-specified number of css squares 使用浏览器javascript,如何获取用户指定本地目录的文件和子文件夹的内容 - Using browser javascript, how to get the contents of the files and subfolders of the user-specified local directory 如何修改momentjs日历以使周从用户指定的一天开始? - How to modify a momentjs calendar to have weeks start on a user-specified day? setTimeout不等待指定的时间 - setTimeout not waiting specified time 在 IE9 中将用户指定的文件作为字符串读取 - Reading a user-specified file as a string in IE9 通过用户指定的文件名以Javascript打开文件 - Open file in Javascript from user-specified file name 如何使用setTimeOut在Javascript中的指定时间段中更改文本? - How to change text in specified time blocks in Javascript using setTimeOut? 当期望 Promise 在 Jasmine 中设置超时时,如何避免测试时间 - How to avoid time in a test when expecting a Promise with setTimeout in Jasmine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM