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.
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];
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.
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.
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.
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();
});
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();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With