How to test a function which has a setTimeout with jasmine?

The overall approach varies based on your Jasmine version. Jasmine 1.3 You can use waitsFor: it( “Disable all submit buttons”, function() { // Get a button var $button = $( ‘#ai1ec_subscribe_users’ ); // Call the function utility_functions.block_all_submit_and_ajax( $button.get(0) ); // Wait 100ms for all elements to be disabled. waitsFor(‘button to be disabled’, function(){ var found … Read more

What is setTimeout doing when set to 0 milliseconds?

A few useful facts might help clarify what’s happening: JavaScript is single-threaded. Asynchronous callbacks are assigned to a message placed in a message queue. When no code is currently executing, the event loop polls the message queue, requesting the next message in line to be processed (executed). setTimeout adds a message (with the callback provided) … Read more

NodeJS Timeout a Promise if failed to complete in time

Native JavaScript promises don’t have any timeout mechanism. The question about your implementation would probably be a better fit for http://codereview.stackexchange.com, but a couple of notes: You don’t provide a means of actually doing anything in the promise, and There’s no need for clearTimeout within your setTimeout callback, since setTimeout schedules a one-off timer. Since … Read more

setTimeout in React Native

Classic javascript mistake. setTimeout(function(){this.setState({timePassed: true})}, 1000) When setTimeout runs this.setState, this is no longer CowtanApp, but window. If you define the function with the => notation, es6 will auto-bind this. setTimeout(() => {this.setState({timePassed: true})}, 1000) Alternatively, you could use a let that = this; at the top of your render, then switch your references to … Read more