javascript - setTimeout不启动函数

标签 javascript jquery settimeout

我有一个使用 setInterval() 方法来观察 css 属性更改的函数(因为在某些浏览器中我们没有事件来执行此操作),现在我需要编写一个单元测试对于这个功能。

var counter = 0;
function callback() {
    counter++;
}
$('body').append('<div id="testDom" style="color:red"/>');
dom = $('#testDom').get(0);
watcherId = DomWatcher.watch(dom, 'color', $.proxy(callback,this));
function testWatch() {
    $(dom).css('color', 'green');
    setTimeout(assertEqual(counter, 1), 1000);
}

然后断言失败。 我确信 watch 功能运行良好,它每 100 毫秒检查一次 css 属性更改。只是没有想出编写单元测试的好方法..

最佳答案

您立即调用 assertEqual 并将其返回值传递给 setTimeout。您需要将一个函数传递给setTimeout

function assert() {
    assertEqual(counter, 1);
}
setTimeout(assert, 1000);

关于javascript - setTimeout不启动函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9818308/

相关文章:

javascript - 使用 Chart.js 显示图表

javascript - jquery:在 html 部分(带有 id)周围添加代码,没有直接编辑选项

javascript - 代码执行的同步延迟

Javascript:如何清除非全局(封闭)setTimeout?

javascript - jQuery - 使用全局数组并等待 $get 加载检索到的数据?

javascript - 在 Kendo Grid 中,当用户在 "items per page"下拉列表中选择特定值时如何添加警报?

javascript - 为动态生成的 div 多次调用单击操作

javascript - 您如何在原型(prototype) 1.5.1 中向上/向下移动兄弟元素的顺序?

javascript - jQuery:使用正确的选择器在对象上未定义

jquery - 在 Html 中修复图像标记中的图像