Search code examples
jqueryajaxjsonmockingmockjax

Any one help me to delay my json request using mockjax..?


In my application some error occurring because of the delays of the ajax call. But i am working on localhost. but still i am getting the data from server, but I don't have as much as data my server has.

So my ajax call takes just milliseconds to return the data. I decide to delay my ajax call in my localhost, for that i selected mockjax plugin. But really i am not able to make any delay on my ajax calls.

here is my try:

var URL = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";

$.mockjax({
    url: URL,
    responseTime: 5000, // it is not delay the call at all..
    responseText: {
        status: 'success',
        fortune: 'Are you a turtle?'
    }
});

$.getJSON(URL, {
    tags: "flowers",
    tagmode: "any",
    format: "json"
},

function (data) {
    $.each(data.items, function (i, item) {
        $("<img/>").attr("src", item.media.m).appendTo("#images");
        if (i == 10) return false;
    });
});

I am intensionally delay my response as 5000 mil seconds, but i am not able to delay it at all.. any one help me to use this plugin please..?

Live Demo


Solution

  • I found modifying the URL to a fake one helped - however to make your page still work with the data I had to insert the sample data returned by the api call to flickr.

    http://jsfiddle.net/uXgX9/4/

    See the fiddle for full code

    Javascript:

    startTime = new Date();
    $.mockjax({
        url: URL,
        responseTime: 5000,
        responseText: {
            "title": "Recent Uploads tagged flowers",
            "link": "http://www.flickr.com/photos/tags/flowers/",
            "description": "",
            "modified": "2013-11-07T12:05:59Z",
            "generator": "http://www.flickr.com/",
            "items": [{
                "title": "La saison d'été décline",
                 ....
    }]
        }
    });
    
    $.getJSON(URL, {
        tags: "flowers",
        tagmode: "any",
        format: "json"
    },
    
    function (data) {
        endTime = new Date();
        diffTime = new Date(endTime - startTime).getSeconds();
        console.log('mocked ' + diffTime + ' seconds later:', data);
        $.each(data.items, function (i, item) {
            $("<img/>").attr("src", item.media.m).appendTo("#images");
            if (i == 10) return false;
        });
    });