Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Hi guys,
is there any way to add a delay or sleep function (e.g. wait for 5 seconds before calling a script) in liquid? E.g. {% sleep(5000) %}?
Thanks,
Patrick
Hello , @premmels
There is no way to do this using liquid code , but you can use JavaScript's setTimeout function to delay code execution
For ex:-
setTimeout(function(){
// Write your code here
}, 5000);
Sorry for the late reply. It seems that my notifications were turned of.
How would I implement that into a liquid code? In this case it's about the checkout area in the settings which will let me add code to the thank you page where I want to delay a certain part of the liquid code for 5 seconds.
Hi premmels, were you able to figure out how to implement this into your liquid code? I am trying to add a feature like this to my cart.liquid
For everyone coming across this issue: there is not a way to implement sleeping or delaying in liquid.
If you intend to add a delay to any part of your code, you must use Javascript, preferably with the use of asynchronous functions (like shared in the previous comment) instead of callbacks (e.g. setTimeout).
Naturally having experience coding with Javascript is crucial for this. Unfortunately this is not something that can be implemented with step-by-step guidance, each theme will require a different approach, and, depending of one's theme, some parts will need to be restructured to be able to fit a delay.
If you're not comfortable with Javascript you'll want to contact a developer to implement this functionality.
Kind regards,
Diego
@premmels
You can't achieve that in liquid, only in Javascript. You can use an async function to wait for a period of time. I use this one:
async function helloSleepy(){
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
console.log("hello");
await sleep(1000);
console.log("hello again");
}
helloSleepy();
Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025Unlock the potential of marketing on your business growth with Shopify Academy's late...
By Shopify Mar 12, 2025Learn how to increase conversion rates in every stage of the customer journey by enroll...
By Shopify Mar 5, 2025