我想在第一个def route: Route = {
import CorsDirectives._
import Directives._
// Your CORS settings
val corsSettings = CorsSettings.defaultSettings
// Your rejection handler
val rejectionHandler = corsRejectionHandler withFallback RejectionHandler.default
// Your exception handler
val exceptionHandler = ExceptionHandler {
...
}
// Combining the two handlers only for convenience
val handleErrors = handleRejections(rejectionHandler) & handleExceptions(exceptionHandler)
// Note how rejections and exceptions are handled *before* the CORS directive (in the inner route).
// This is required to have the correct CORS headers in the response even when an error occurs.
handleErrors {
cors(corsSettings) {
handleErrors {
... // your business route here
}
}
}
}
我不确定如何应用此解决方案: How to add pause between each iteration of jQuery .each()?
根据我的具体情况:
$.each
答案 0 :(得分:1)
你可以将你的iterateAddresses()函数
中的其他函数分开function iterateAddresses () {
var time = 500;
// Then get a random number between 500 and 599
$.each( addresses_google, function( index, value ) {
var ram = Math.floor(Math.random() * (599-time+1)) + time;
setTimeout( "OtherFunction("+index+","+value+");", ram);
}); // end each google_address
}; // end iterateAddresses
function OtherFunction(index, value)
{
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [origin],
destinations: value,//addresses_google,
travelMode: 'DRIVING'
},
callback
);
function callback(response, status) {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++) {
var element = results[j];
if(element.status == "NOT_FOUND"){
var distance = 0;
} else {
var distance = element.distance.value;
}
var from = origins[i];
var to = destinations[j];
if(distance > radius){
// Store postids in array
postids_to_hide.push(
addresses_postids[index][j]
);
// Hide elements where postid is in the postids_to_hide arrays
$.each( postids_to_hide, function( index, value ) {
$(".main_short_post_div").filter(function(){
return $(this).attr('data-post-id') === value;
}).hide();
});
} // end if d < r
} // end for j
} // end for i
} // end callback function
}
iterateAddresses();
答案 1 :(得分:1)
function asyncForEach(arr, cb) {
return arr.reduce((p,c)=>{
return p.then(()=>cb(c));
}, Promise.resolve());
}
function wait(ms) {
return ()=>new Promise(resolve=>setTimeout(resolve, ms));
}
const DELAY = 500; //ms
function iterateAddresses() {
return asyncForEach(addresses_google, address =>
getDistanceMatrix(address)
.then(processResult)
.then(wait(DELAY)));
}
iterateAddresses();
getDistanceMatrix
是您提供给$.each
的函数的promisified版本。
processResult
是callback
的宣传版。