Creating javascript conditions

I am new to javascript and am adapting a codebase, I have two conditions within my spin function, one says that you can ONLY spin the wheel once and the other says that you may not spin the wheel with an empty email field.

An unintended consequence (its not a bug because the conditions are doing exactly what they are asked) is that if you don’t enter an email and “click” the spin button your still engaging the “only spin once” condition.

I need to create a condition that a spin (.click) isnt valid unless the email address is created, i am not sure how to do this, here is my relevant code snippet:

/*WHEEL SPIN FUNCTION*/
	$('#spin').one('click', function(){
     	if ($("#login").val() === "") {
    return;
}

Seems to me that you have one click event which needs to check two things.

Can you just do the email validation check first and only then engage the “spin once” condition? Do them both in the same click event?

You might use a regular .on(), and if the email field is not empty, remove the listener manually using .off():

$('#spin').on('click', function handleSpin () {
  if ($('#login').val() === '') {
    return
  }

  $(this).off('click', handleSpin)
})
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.