Solved

jquery mobile flip switch

Posted on 2014-11-07
1
627 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Send form to asp server side 6 26
How Close unsubmited attempts 10 43
Triple line in an alert message 4 20
How do I post more than 1 item to php backend 24 29
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…
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…
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)
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…

735 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