Like i explain here , i can\'t use window.setTimeout() anymore and any window classical functions like clearInterval etc ...); but i need calling a JS block code as an async one
Try using jQuery version 3.0 .animate(), which now uses requestAnimationFrame
// Creates a jQ object where elem set to index of [0]
// a plain object with value of 0 `{to:0}`
// call .animate() chained to the jQ object
// Animates `{to:0}` value from 0 - 1
// $({to:0}).animate({to:1}
var duration = 5000;
$({to:0}).animate({to:1}, duration, function() {
// do stuff after `duration` elapsed
$("#messageTimer").html("Happy New Year ! (working version)")
})
<script src="https://code.jquery.com/jquery-3.0.0-beta1.min.js"></script>
<div id="messageTimer"></div>
What about something like this?
var delay = 10000; // milliseconds
var before = Date.now());
while (Date.now() < before + delay) {};
alert('The delay has passed!');
Also, you might be interested in the Promise objects. Which could give you somtehing like this :
var customDelay = new Promise(function(resolve) {
var delay = 10000; // milliseconds
var before = Date.now();
while (Date.now() < before + delay) {};
resolve('Success!');
});
customDelay.then(function(msg) {
document.getElementById("messageTimer").innerHTML = "Happy New Year !';
});
---EDIT--- The Promise object is part of ECMAScript 6 so there will be backward compatibility issues.
Luckily for you, jQuery does have it's own implementation of Promises! Introduction to jQuery Promises
See this page for documentation. Implementation will be similar.