10 examples of 'jquery wait for function to finish' in JavaScript

Every line of 'jquery wait for function to finish' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
49function waitsFor(callback) {
50 if (skipRunsCounter === 0) {
51 jasmineApi.waitsFor.apply(this, arguments);
52 }
53}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

84export function waitFor(timeout, done, onCallback, onTimeout) {
85 if (typeof onCallback !== 'function') {
86 onCallback = onCallback
87 ? function (r) { void expect(r).to.be.exist; }
88 : function (r) { expect.fail('unexpected callback: ' + r); };
89 }
90 if (typeof onTimeout !== 'function') {
91 onTimeout = onTimeout
92 ? function () {}
93 : function () { expect.fail('timeout'); };
94 }
95 let to = setTimeout(() => {
96 try {
97 to = null;
98 onTimeout();
99 } catch (e) {
100 return done(e);
101 }
102 return done();
103 }, timeout);
104 return function () {
105 if (to == null) return;
106 try {
107 clearTimeout(to);
108 to = null;
109 onCallback.apply(this, arguments);
110 } catch (e) {
111 return done(e);
112 }
113 return done();
114 };
115}
139async wait(pageCompleteCheck) {
140 const waitTime = this.options.pageCompleteWaitTime || 5000;
141 if (!pageCompleteCheck) {
142 pageCompleteCheck = this.options.pageCompleteCheckInactivity
143 ? pageCompleteCheckByInactivity
144 : defaultPageCompleteCheck;
145 // if using SPA just override
146 if (this.options.spa) {
147 pageCompleteCheck = spaCheck;
148 }
149 }
150
151 const driver = this.driver,
152 pageCompleteCheckTimeout = this.options.timeouts.pageCompleteCheck;
153 try {
154 const pageCompleteCheckCondition = new Condition(
155 'for page complete check script to return true',
156 function(d) {
157 return d.executeScript(pageCompleteCheck, waitTime).then(function(t) {
158 return t === true;
159 });
160 }
161 );
162 log.debug(
163 `Waiting for script pageCompleteCheck at most ${pageCompleteCheckTimeout} ms`
164 );
165 log.verbose(`Waiting for script ${pageCompleteCheck}`);
166 await timeout(
167 driver.wait(
168 pageCompleteCheckCondition,
169 pageCompleteCheckTimeout,
170 undefined,
171 this.options.pageCompleteCheckPollTimeout || 200
172 ),
173 pageCompleteCheckTimeout,
174 `Running page complete check ${pageCompleteCheck} took too long `
175 );
176 log.debug(`Waiting after load event for ${waitTime} ms.`);
177 await delay(waitTime);
178 } catch (e) {
179 log.error('Failed to wait ' + e);
180 throw new UrlLoadError('Failed to wait ', {
181 cause: e
182 });
183 }
184}
66(function wait() { setTimeout(wait, 5000) })();
147export async function waitFor(milliseconds: number): Promise<void> {
148 /* istanbul ignore next */
149 return new Promise(resolve => setTimeout(() => resolve(), milliseconds));
150}
41function waitForElement(selector, callback) {
42 if (document.querySelector(selector)) {
43 return callback()
44 } else {
45 setTimeout(waitForElement.bind(this, selector, callback), 300)
46 }
47}
118async function waitTestsToFinish(page, timeoutInMillis) {
119 let started = performance.now(),
120 passed,
121 stillRunning;
122
123 console.info('waiting for tests to finish...');
124 do {
125 stillRunning = (await page.$$('.status.running')).length;
126 console.info('found ' + stillRunning.length + ' running tests');
127 await new Promise(resolve => setTimeout(resolve, 100));
128 passed = performance.now() - started;
129 } while (stillRunning > 0 && passed < timeoutInMillis);
130
131 if (!stillRunning) {
132 console.info('tests done in ' + passed + 'ms');
133 } else {
134 console.error('timed out after ' + passed + 'ms');
135 }
136}
209function waitForElement(selector, callback) {
210 waitFor(function() {
211 return !!$(selector).length;
212 }, callback);
213}
253async waitFor (func, pollMs=100) {
254 return new Promise(resolve => {
255 var poll = async () => {
256 var result = await func()
257 if (result) {
258 resolve(result)
259 } else {
260 setTimeout(poll, pollMs)
261 }
262 }
263 poll()
264 })
265}
88function waitFor(start, ival, done) {
89 setTimeout(function () {
90 if (done()) return
91 var clear = setInterval(function () {
92 if (done()) {
93 clearInterval(clear)
94 }
95 }, ival)
96 }, start)
97}

Related snippets