vendredi 5 mai 2017

How to make an AJAX call hang

I'm trying to test the behavior of my site when scripts take too long to load. I know how to completely block the request so that it returns a 404 by adding something to the host file, but not sure how to force the request to just not complete.

Example,

127.0.0.1 translate.google.com # this blocks google translate from loading (404)

Instead, I'd like to make translate.google.com not respond for x seconds.

I'm using requirejs to load my scripts and I'm noticing that timeout behaves differently than 404.

I added a module to allow google translate to be optionally required, and when I block it via host file, the site behaves correctly. When the script times out, the site fails to load.

// http://ift.tt/2phcXqy

define("optional", [], {
    load: function (moduleName, parentRequire, onload, config) {
        var onLoadSuccess = function (moduleInstance) {
            // Module successfully loaded, call the onload callback so that
            // requirejs can work its internal magic.
            onload(moduleInstance);
        }

        var onLoadFailure = function (err) {
            // optional module failed to load.
            var failedId = err.requireModules && err.requireModules[0];
            console.warn("Could not load optional module: " + failedId);

            // Undefine the module to cleanup internal stuff in requireJS
            requirejs.undef(failedId);

            // Now define the module instance as a simple empty object
            // (NOTE: you can return any other value you want here)
            define(failedId, [], function () { return {}; });

            // Now require the module make sure that requireJS thinks 
            // that is it loaded. Since we've just defined it, requirejs 
            // will not attempt to download any more script files and
            // will just call the onLoadSuccess handler immediately
            parentRequire([failedId], onLoadSuccess);
        }

        parentRequire([moduleName], onLoadSuccess, onLoadFailure);
    }
});

Aucun commentaire:

Enregistrer un commentaire