Solved

jquery mobile flip switch

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Recently, we decided that in order to stay competitive in today's IT world, we were going to have to develop and deploy a mobile app for our users and do it quickly. We felt that we were already behind the curve. Users of technology today are mov…
Read about why website design really matters in today's demanding market.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

816 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

12 Experts available now in Live!

Get 1:1 Help Now