I am trying to pause in the middle of a function for a given period of time (5 secs)
this is what I tried, but It doesn’t work
(function MessagesAjax() {
$.post('/api/messages/get', function(data) {
for (var i = 0; i < data.length; i++) {
$.jGrowl(data[i].Body.toString().substring(0, 150), { header: 'New Message', sticky: true, });
markDisplayed(data[i].Id);
}
setTimeout(MessagesAjax, 5000);
});
})();
function markDisplayed(id) {
setTimeout(5000); //want it to pause here
$.post("/api/messages/markdisplayed" + id);
console.log("Marking displayed");
}
I just need it to pause so there is a delay before the post is sent back to the server
I’m almost sure that setTimeout is not what I want, but I’m not sure what else to use
Change markDisplayed to:
This will cause the function to wait for five seconds before executing the content within it.