Nathaniel Meyer Nathaniel Meyer - 1 month ago 8
HTML Question

Disable a Function jQuery

On Resize, I either call desktop Or mobile. I have a click function in each and they both seem to be running, if it was in desktop and then changes to mobile. I need to turn off desktop function if the mobile function is true and vice versa.

Here is a basic version of my code.

Screen_size()

$(window).resize(function() {
Screen_size()
});

function Screen_size(){
var w = $(window).width();
if (w <= 710){
Mobile();
}

if (w > 710){
Desktop();
}
}

function Mobile(){
console.log('Mobile');
$( "#open" ).click(function(e) {
console.log('Open Mobile');
});
}

function Desktop(){
console.log('Desktop');
$( "#open" ).click(function(e) {
console.log('Open Desktop');
});
}

Answer

No need for window resize .. just make a click event like this

$( "#open" ).on('click', Screen_size);
    
function Screen_size(){
  var w = $(window).width();
  (w <= 710) ? Mobile() : Desktop() ;
}

function Mobile(){
  console.log('Open Mobile');
}

function Desktop(){
  console.log('Open Desktop');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="open">Click</button>

Comments