How to reset prize in Spinning Wheel every 30 seconds

Hi Max, that application would actually solve my problem as well. I tried to use that code with a fraction of an hour in order to get it to refresh after 30 seconds, but it didn’t work. Can you provide an adaptation for that to work in a shorter time interval?

2 Likes

Hi there, @Fyzical_Therapy :waving_hand:

We’ve slightly adjusted your script in the Custom JS field and now the rest should occur every 30 seconds:

const WIDGET_ID = '03b9cb00-0ab6-41af-bcb6-3efa8ce9af9b';
const LOCAL_STORAGE_SELECTOR = `SpinningWheel.winningRewardId.${WIDGET_ID}`;
const SESSION_STORAGE_SELECTOR = `elfsight-spinning-wheel-${WIDGET_ID}`;
const RESET_AFTER_HOURS = 0.0083;

const waitForElement = (selector, root = document) =>
  new Promise((res) => {
    let i = 0;

    const check = () => {
      const component = root.querySelector(selector);

      if (component) {
        res(component);
      } else if (i !== 50) {
        setTimeout(check, 100);
        i++;
      }
    };

    check();
  });

waitForElement(`.elfsight-app-${WIDGET_ID}`).then(() => {
  const lsValue = localStorage.getItem(LOCAL_STORAGE_SELECTOR);

  if (!lsValue) {
    sessionStorage.removeItem(SESSION_STORAGE_SELECTOR);
    return;
  }

  try {
    const parsedValue = JSON.parse(lsValue);

    if (
      parsedValue &&
      typeof parsedValue === 'object' &&
      typeof parsedValue.value === 'string' &&
      typeof parsedValue.timestamp === 'number'
    ) {
      const currentTimestamp = Math.floor(Date.now() / 1000);
      const timestamp = parsedValue.timestamp;
      const resetThreshold = RESET_AFTER_HOURS * 60 * 60;

      if (currentTimestamp - timestamp >= resetThreshold) {
        localStorage.removeItem(LOCAL_STORAGE_SELECTOR);
        sessionStorage.removeItem(SESSION_STORAGE_SELECTOR);
      }
    }
  } catch (e) {
    console.error('Error parsing localStorage value:', e);
  }
});

Please check it out and let me know if it helped :wink:

1 Like

Thanks Max!

Does this only work with an embed though? It doesn’t seem to work when I use the share link - https://03b9cb000ab641afbcb63efa8ce9af9b.elf.site

2 Likes

The code should work with the Share Links too. I’d like to note that the reset occurs only if you reload the page.

I’ve tested your share link and everything worked fine. Could you please check it?

I see now, OK thank you!

1 Like

You’re always welcome :slightly_smiling_face: