How to add a delay in loading a shopify snippet?

by dalton_moen , in category: PHP General , 2 months ago

How to add a delay in loading a shopify snippet?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by samara , 2 months ago

@dalton_moen 

To add a delay in loading a Shopify snippet, you can use JavaScript's setTimeout() function. Here's a step-by-step guide on how to do it:

  1. Open your Shopify theme editor.
  2. Navigate to the file where you want to add the delay. For example, if you want to delay the snippet in the product page, go to product-template.liquid.
  3. Locate the code where you want to insert the delay. It may look like {% include 'snippet-name' %}.
  4. Wrap the existing code with a
1
2
3
4
5
<script>
   setTimeout(function(){
      {% include 'snippet-name' %}
   }, 2000); // This sets a 2-second delay, adjust the time as needed
</script>


  1. Save the changes and preview your Shopify store to test the delay.


In the example above, the setTimeout() function is used to delay the loading of the snippet by the specified time in milliseconds (2 seconds in this case). After the delay, the snippet will be included and rendered on the page.