Reset the prize

We would like to have a way to reset the prize and spin the wheel every day

2 Likes

Hi there, @Tuna_Homestay :wave:

Sounds interesting! If this request becomes popular, it might be considered in the future :slightly_smiling_face:

1 Like

Guys, if you’d like to implement this feature for your widget, it’s possible to do this with the custom code.

Just let us know in the comment and our devs will be happy to help!

1 Like

Yes, I would like the wheel to automatically reset on a daily or even hourly basis, allowing users to spin again after each transaction. Could you please assist us with setting this up?

2 Likes

Hi there, @Evaldas_Einikis :wave:

Please add this code to the Custom JS field on the Settings tab of your widget’s settings:

const WIDGET_ID = 'YOUR_WIDGET_ID';
const LOCAL_STORAGE_SELECTOR = `SpinningWheel.winningRewardId.${WIDGET_ID}`;
const SESSION_STORAGE_SELECTOR = `elfsight-spinning-wheel-${WIDGET_ID}`;
const RESET_AFTER_HOURS = 15;

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

Note: This code resets the prize on daily basis.


Replace YOUR_WIDGET_ID in the 1st line of the code with the ID of your widget and let me know if it helped :slightly_smiling_face:

1 Like

Hello Max,

The code brings the following error. If there is a bigger problem with both requirements, for now it would be sufficient for us to solve just the wheel reset problem so we can go live. Can you advise us on the code please.

Line 24: ‘container’ is defined but never used.

const WIDGET_ID = ‘8d7bf735-5ce7-4900-9d1d-5e993c88f399’;

const LOCAL_STORAGE_SELECTOR = SpinningWheel.winningRewardId.${WIDGET_ID};

const SESSION_STORAGE_SELECTOR = elfsight-spinning-wheel-${WIDGET_ID};

const RESET_AFTER_HOURS = 24;

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((container) => {

const lsValue = localStorage.getItem(LOCAL_STORAGE_SELECTOR);

if (!lsValue) {

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

}

});

2 Likes

Hi @Evaldas_Einikis :wave:

This error doesn’t affect anything and everything should be working fine. However, we’ve adjusted the code and this error won’t appear anymore:

const WIDGET_ID = '8d7bf735-5ce7-4900-9d1d-5e993c88f399';
const LOCAL_STORAGE_SELECTOR = `SpinningWheel.winningRewardId.${WIDGET_ID}`;
const SESSION_STORAGE_SELECTOR = `elfsight-spinning-wheel-${WIDGET_ID}`;
const RESET_AFTER_HOURS = 15;

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) {
    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 worked :slightly_smiling_face:

1 Like

Hi, I implemented the code and set the reset 1 hour:
RESET_AFTER_HOURS = 1; After more than one hour I still could not spin the wheel.

Evaldas

1 Like

Hello Max,

no it is working. Maybe some time needed to pass. Thanks a lot.

2 Likes

Great, you’re welcome :slightly_smiling_face:

1 Like

Hello Max, what can I do if there is no Custom JS field and only a Custom CSS code field?

2 Likes

Hi @mystaceous :waving_hand:

Custom JS field is available in all widgets, including yours. You can find it on the Settings tab :slightly_smiling_face:

1 Like

Sorry about that, I was looking at options within my website, not the app. I appreciate the help!

2 Likes

No problem!

You’re always welcome :wink:

2 Likes