How can I implement a delay function in liquid?

How can I implement a delay function in liquid?

premmels
Excursionist
19 0 4

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

Replies 5 (5)

IttantaTech
Shopify Partner
525 55 103

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); 

Thanks,
Ittanta Technologies Pvt. Ltd. | Shopify Expert
If reply is helpful, please Like and Accept Solution.
To hire us, contact us at info@ittanta.com
premmels
Excursionist
19 0 4

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.

jhphoenix
Visitor
1 0 0

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

diego_ezfy
Shopify Partner
2971 571 923

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

diego_ezfy
Shopify Partner
2971 571 923

@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();