Solved

jquery mobile flip switch

Posted on 2014-11-07
1
560 Views
Last Modified: 2014-11-14
I am using a jquery mobile flip switch.

I have the "change" event bound to it, so that when the user changes it, it sends an ajax request to the server to change the state of the switch.

$("#myswitch").bind("change", function (event){
    // do ajax call to server
});

The client also polls the server, and If a different users on another browser somewhere changes the flip switch, then the switch on this browser is changed programatically:

$('#myswitch').val('on').slider("refresh");

The problem is that this slider refresh causes the change event to fire, when then sends ajax back to the server, which results in a loop.

Is there a way to know in the change event whether the event was fired because the user actually changed the switch, or if the switch was changed programmatically?
0
Comment
Question by:rustycp
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40430030
try this :
$("#myswitch").bind("change", function (event){
    if( !event.isTrigger ) {  // if not programmatically
        // do ajax call to server
    }
});

Open in new window

or this :
$("#myswitch").bind("change", function (event){
    if( event.originalEvent ) {  // if not programmatically
        // do ajax call to server
    }
});

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now