Solved

jquery mobile flip switch

Posted on 2014-11-07
1
578 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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…

910 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

22 Experts available now in Live!

Get 1:1 Help Now