Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show element only if getJSON takes more than n milliseconds?

I have some JavaScript:

surveyBusy.show();

$.getJSON(apiUrl + '/' + id)
    .done(function (data) {
        ...
        surveyBusy.hide();
    })
    .fail(function (jqXHR, textStatus, err) {
        ...
        surveyBusy.hide();
    });

However, I'd like to only issue surveyBusy.show(); if $.getJSON takes more than n number of milliseconds. You get a flicker otherwise. Is there a callback on the getJSON api that can do this? I see nothing in the documentation.

like image 235
Mike Perrenoud Avatar asked Aug 09 '13 13:08

Mike Perrenoud


People also ask

How to find elements that appear more than n/k times?

A general solution for searching elements that appear more n/k times. - LeetCode Discuss A general solution for searching elements that appear more n/k times. It's based on Moore Voting Algorithm. For the question majorityElement ( finding an element that appears > n/2), return (_majorityElementOfK (nums, 3)) [0];

How to print only the elements that appear more than once?

After storing counts, we traverse input array again and print those elements whose counts are more than once. To make sure that every output element is printed only once, we set count as 0 after printing the element. // those elements that appear more than once.

How do I find the majority of elements that appear > n/2?

For the question majorityElement ( finding an element that appears > n/2), return (_majorityElementOfK (nums, 3)) [0]; That's because “num == elements [i]” should be checked separately before "!counters [i]", so that the helper slots will not have duplicates.

How to check if majority element doesn’t exist in array?

If maximum count becomes greater than n/3 then print it. If the maximum count doesn’t become more than n/3 after the traversal of array then the majority element doesn’t exists. # This code is contributed by mukesh07.


2 Answers

Just use a timeout. Also, I put your "hide" code in the always handler to reduce code repetition.

var busyTimeout = setTimeout(function() { surveyBusy.show(); }, 2000);

$.getJSON(apiUrl + '/' + id)
    .done(function (data) {
        ...
    })
    .fail(function (jqXHR, textStatus, err) {
        ...
    })
    .always(function() {
        clearTimeout(busyTimeout);        
        surveyBusy.hide();        
    });
like image 56
Jason P Avatar answered Oct 22 '22 14:10

Jason P


Put your surveyBusy.show() call inside a timeout and then cancel that timeout (using window.clearTimeout) if the result is returned before it activates:

var busyTimeout = window.setTimeout(function() { surveyBusy.show(); }, 500);

$.getJSON(apiUrl + '/' + id)
    .done(function (data) {
        ...
    })
    .fail(function (jqXHR, textStatus, err) {
        ...
    })
    .always(function() {
        window.clearTimeout(busyTimeout);        
        surveyBusy.hide();        
    });
like image 26
Richard Dalton Avatar answered Oct 22 '22 13:10

Richard Dalton