Tag: settimeout

许多setTimeouts的后果

我正在构build一个用户可以生成自定义计时器的页面。 这些定时器通过setTimeout在一个heroku nodejs后端运行。 然而,我有点担心可能有几千个长时间的setTimeout同时运行。 我发现这个问题,其中指出JavaScript的计时器是在浏览器中的一个单独的线程上运行。 在nodejs中的情况是一样的吗? 如果是这样,在这个线程上有数千个定时器会有什么后果呢? 编辑:关于我想要做什么的更多信息。 我正在构build一个同步页面,可以放在屏幕上,然后用户可以访问浏览器中的页面并更改其实例的内容。 这将在所有打开的页面实例中反映出来。 用户还应该能够设置定时器,这应该在定时器结束时推送新的内容。 这是我正在考虑使用服务器端setTimeouts来做。 我知道我应该使用一个heroku调度器插件,但我想降低成本(这种页面不完全是一个赚钱)。 最后,用户应该能够生成自己的这个页面的实例,一个新的url等。 这是可扩展性问题的起因。 如果存在100个页面实例,并且每个实例设置了10个定时器,则在后端有1000个定时器。

节点setTimeout notWorking

function work(){ //do some of the heavy computation now process.nextTick(work); } work(); console.log("started"); setTimeout(function(){ console.log("hi there");}, 1000); 当我运行他的代码,我得到:开始,但我从来没有得到消息嗨。 我做错了什么,或者这是节点的问题? 编辑:当我用setTimeoutreplaceprocess.nextTick它的工作原理,但nextTick应该是更快,或者当我用set.out.nextTick()你好显示,但它不会等待1000毫秒。

在settimeout内承诺

我试图在彼此延迟1秒的时间内执行承诺,因为我正在使用的API服务器每秒有一个请求限制。 这是我的代码目前 var delay = 0; return categories.reduce(function(promise, category) { var id = setTimeout(function() { promise.then(function() { return client.itemSearch({ searchIndex: configuration.SearchIndex, CategoryID: category.id, Keywords: currentKeyword }).then(function(results) { var title = results[0].Title; var cat = category.name; var price = results[0].Price; return db.insertProduct(title, cat, price); }); }).catch(function(err) { console.log("error", err); }); }, delay * 1000); delay += […]

NodeJS clearTimeout()似乎不能正常工作?

我有一个超时,它基本上杀死了这个进程运行了很长时间,但是这只有当某个条件没有被满足的时候。 如果我的条件被击中,我使用clearTimeout()来清除超时,但似乎错误和进程杀死持续。 这是我的代码片段; 即使clearTimeout()命中,错误仍然会在5分钟后打印出来。 function poll(callback){ var timeLimit = setTimeout(function(){ if(log) log("Could not get a response from the command.", "error"); process.exit(1); }, 300000); exec("some command", function(error, stdout, stderr){ if(stdout){ clearTimeout(timeLimit); // Do other stuff } else { setTimeout(function(){ poll(callback); }, 1000); } }); } 我在浏览器中testing,似乎在一个典型的JavaScript控制台工作,但也许我上面做错了什么? 如果是这样,任何帮助将不胜感激。 以上是我的确切代码(当然除了命令和错误消息),所以如果有任何错误,应该可以看到上面。 提前致谢。

如何在Node.js中触发超时function?

我想触发setTimeoutcallback函数,但似乎不工作。 有什么问题? var fs = require("fs"); // set timeout callback setTimeout(function(){ console.log("5000ms timeout"); process.exit(0); }, 5000 ); // do something more than 5000ms while(true) { var stats = fs.statSync("foo"); console.log("while statement running…"); } 当我运行这个,5秒后,程序仍在运行

如何暂停或等待在摩卡testing案例 – setTimeout不工作

我build了一个selenium摩卡testing用例打开谷歌,input一些文本,然后单击search。 这是我的selenium摩卡testing用例代码片段 但是我只需要在inputsearch文本60秒后执行点击search var assert = require('assert'), test = require('selenium-webdriver/testing'), webdriver = require('selenium-webdriver'); var urladd ='http://www.google.com/'; function clickLink(link){ link.click(); return link; } var testSimple=function(searchContent){ var browser = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build(); test.describe('\n\nGoogle Search\n', function() { this.timeout(60000); test.it('Enter element to be searched', function(done) { browser.get(urladd); browser.findElement(webdriver.By.name('q')).sendKeys(searchContent); browser.getTitle().then(function(title) { assert.equal(title,'Google'); }) }); test.it('Click search button', function(done) { setTimeout(function(){ browser.findElement(webdriver.By.name('btnG')).then(clickLink).then(function(){ […]

setTimeout函数和asynchronous函数

这是关于如何setTimeout执行其callback。 我有以下几点 function f1 (argument) { console.log('f1 start'); for(var i = 0; i < 100000; ++i) for(var j = 0; j < 10000; ++j); console.log('f1 complete'); } function f2 (argument) { console.log('f2 start'); for(var i = 0; i < 1000; ++i) for(var j = 0; j < 10000; ++j); console.log('f2 complete'); } function f3 (argument) […]

按顺序调用JavaScript代码的另一种方法是在两者之间拖延

我有这个代码最初在Python中。 SendSerialPortCommand("XXX") time.delay(0.5) SendSerialPortCommand("YYY") 我把这个代码转换为node.js,但代码看起来更丑。 SendSerialPortCommand("XXX"); setTimeout(function () { SendSerialPortCommand("YYY"); }, 500); 想象一下,如果我的Python代码看起来像这样。 SendSerialPortCommand("XXX") time.delay(0.5) SendSerialPortCommand("YYY") time.delay(0.5) SendSerialPortCommand("AAA") time.delay(0.5) SendSerialPortCommand("BBB") 使用setTimeout()内的setTimeout() ,node.js代码看上去真的很难看。 怎样才能改善node.js代码的可读性? 我不在乎这个问题违反JavaScript的asynchronous性质。 重要的是可读性。

setTimeout不能和node.js一起工作

我正在写摩卡testing用例来testing以下步骤。 我打算做一个API调用,并等待30分钟,然后再调用另一个API。 我正在使用一个内部节点API编写来调用REST API来编写这个testing用例。 但由于某些原因, setTimeout并不等待给定的ms。 有人可以帮帮我吗? describe('Checkout – ', function() { before(function() { lapetus = test.Lapetus; }); it('Get purchase contract after session is expired [C123]', function(done) { this.timeout(180000000); lapetus.run(function() { // create customer …… // create new cart and add one item …… // create new contract with empty cart id ……. var pc_id […]

无法在Node.js循环中设置超时

在这里,我试图设置每个迭代超时,但我不能让它因为nodejs的性质。 有没有办法做到这一点? 提前致谢 for (var i = 1; i <= 1000; i++) { setTimeout(function () { console.log('something') }, 3000); }