Skip to content

Commit

Permalink
Improvement/timeout xhr (#1172)
Browse files Browse the repository at this point in the history
* implement timeout on ajax requests

* missing semi-colon
  • Loading branch information
Matt Kendall authored May 18, 2017
1 parent 694dfba commit 223b969
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {parse as parseURL, format as formatURL} from './url';
var utils = require('./utils');

const XHR_DONE = 4;
let _timeout = 3000;

/**
* Simple IE9+ and cross-browser ajax request function
Expand All @@ -13,6 +14,9 @@ const XHR_DONE = 4;
* @param data mixed data
* @param options object
*/
export function setAjaxTimeout(timeout) {
_timeout = timeout;
}

export function ajax(url, callback, data, options = {}) {
try {
Expand Down Expand Up @@ -42,6 +46,7 @@ export function ajax(url, callback, data, options = {}) {
}
}

x.timeout = _timeout;
if (useXDomainRequest) {
x = new window.XDomainRequest();
x.onload = function () {
Expand Down
2 changes: 2 additions & 0 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { isValidePriceConfig } from './cpmBucketManager';
import { listenMessagesFromCreative } from './secureCreatives';
import { syncCookies } from 'src/cookie.js';
import { loadScript } from './adloader';
import { setAjaxTimeout } from './ajax';


var $$PREBID_GLOBAL$$ = getGlobal();
Expand Down Expand Up @@ -455,6 +456,7 @@ $$PREBID_GLOBAL$$.requestBids = function ({ bidsBackHandler, timeout, adUnits, a
const timedOut = true;
const timeoutCallback = bidmanager.executeCallback.bind(bidmanager, timedOut);
const timer = setTimeout(timeoutCallback, cbTimeout);
setAjaxTimeout(cbTimeout);
if (typeof bidsBackHandler === objectType_function) {
bidmanager.addOneTimeCallback(bidsBackHandler, timer);
}
Expand Down

5 comments on commit 223b969

@ptomasroos
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must just say, this is really great! Excited to start testing it out in production @mkendall07

@ehoch
Copy link
Contributor

@ehoch ehoch commented on 223b969 May 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome @mkendall07 ! Is there a similar concept we can implement for loadScript? It seems more adapters are using this rather than the ajax library right now.

@mkendall07
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks guys. @ehoch not really a way to do it in loadScript unfortunately. You should encourage your SSP's to use ajax.

@ehoch
Copy link
Contributor

@ehoch ehoch commented on 223b969 May 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prebid 1.0 requirement?

@mkendall07
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is part of the 1.0 adapter requirement.

Please sign in to comment.