Time Out code, Timed Trigger to Auto redirect or Auto navigate to another page or URL

Updated: Dec 10, 2020


About

Time Out codes are generally used in scenarios when you want to automate a trigger based on after a specific amount of time has passed. In this case, we are using the Time Out code to redirect a person to another Wix page or an external URL


Here are some common scenarios of when to trigger the Time Out code:


  • When the page loads

  • When a button is clicked

  • When a dataset is saved

  • After a query or filter has been executed


 

Good to Know #1

The Time


The code is written in milliseconds, therefore the number:


  • 5000 is equivalent to 5 seconds

  • 1000 is equivalent to 1 second

  • 60000 is equivalent to 1 minute

  • 3600000 is equivalent to 1 hour


 

Good to Know #2

The Code to redirect to another Wix page


On Page Load:


import wixLocation from 'wix-location';

$w.onReady(function() {
 const millisecondsToDelay = 5000;
    setTimeout(() => {
        wixLocation.to(`/join`);
    }, millisecondsToDelay);
});


On Button Click:


import wixLocation from 'wix-location';

$w.onReady(function () {
    $w("#myElement").onClick((event) => {
 const millisecondsToDelay = 5000;
        setTimeout(() => {
            wixLocation.to(`/join`);
        }, millisecondsToDelay);
    });
});
    


After Dataset Saved:


import wixLocation from 'wix-location';

$w.onReady(function () {
    $w("#myElement").onAfterSave(() => {
 const millisecondsToDelay = 5000;
        setTimeout(() => {
            wixLocation.to(`/join`);
        }, millisecondsToDelay);
    });
});
    


After Query is executed:


import wixLocation from 'wix-location';
import wixData from 'wix-data';

$w.onReady(function () {
    //
});

// ... some other code that triggered the query

wixData.query("myCollection")
  .find()
  .then( () => {
 const millisecondsToDelay = 5000;
        setTimeout(() => {
            wixLocation.to(`/join`);
        }, millisecondsToDelay);
  } )
  .catch( (err) => {
 let errorMsg = err;
  } );
 


After Filter is executed:


import wixLocation from 'wix-location';
import wixData from 'wix-data';

$w.onReady(function () {
    //
});

// ... some other code that triggered the filter

$w("#myDataset").setFilter( wixData.filter()
  .startsWith("lastName", "D")
)
.then( () => {
  console.log("Dataset is now filtered");
 const millisecondsToDelay = 5000;
        setTimeout(() => {
            wixLocation.to(`/join`);
        }, millisecondsToDelay);
} )
.catch( (err) => {
  console.log(err);
} );
  

 

Good to Know #3

The Code to redirect to an external URL


On Page Load:


import wixLocation from 'wix-location';

$w.onReady(function() { 
 const millisecondsToDelay = 5000; 
    setTimeout(() => {         
        wixLocation.to(