I would like a add a 1-2 second delay on each iteration of the following loop.
<html>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<input id="start" type="submit"> </input>
<div id='status'></div>
<script>
var geocoder=new google.maps.Geocoder();
var glGeocodeCount = 0 ;
$(document).ready(function() {
$('#start').click(function() {
//srPerformGeocode("TD Tower, 55 King Street West, Toronto, ON, Canada, M5K 1A2");
for(x=0;x<20;x++){
srPerformGeocode("TD Tower, 55 King Street West, Toronto, ON, Canada, M5K 1A2");
}
return false;
});
});
function srPerformGeocode(address){
if (geocoder){
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK){
$('#status').prepend("Success : " + address + "<br/>");
}
else{
$('#status').prepend("Failed : " + address + "<br/>");
}
});
}
}
</script>
Formatting slow motion in a script Select the “Action” icon at the top toolbar. Otherwise create a new line of action description to begin the slow motion writing sequence. Simply write “SLOW MOTION:” followed by whatever action you intend to be shown in slow motion.
The standard way of creating a delay in JavaScript is to use its setTimeout method. For example: console. log("Hello"); setTimeout(() => { console.
Conclusion. The for , while , and do-while loops all have similar performance characteristics, and so no one loop type is significantly faster or slower than the others.
Modern JS Solution:
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
async function slowedCode() {
console.log("Before Delay")
await this.timeout(Math.random() * 2000 + 500) // Wait random amount of time between [0.5, 2.5] seconds
console.log("After Delay")
}
async function slowedForLoop() {
const data = ["1","2","3","4","5"]
for (let d of data) {
console.log(d)
await this.timeout(Math.random() * 100 + 500)
}
}
The only draw back is you have to execute the delay from inside an async function.
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