Solved

If/Else Statements Socket.io Client

Posted on 2013-12-14
1
236 Views
Last Modified: 2013-12-14
I have been trying to add an If/Else statement to my Socket.io client for a personal project I've been working on. The end goal is to create a Bitcoin ticker with 2 values (oldPrice and newPrice) sent from the server. I would like to be able to change the styling and bg color depending on the values.

This is what I have to far:

var socket = io.connect('http://localhost:1000');

socket.on('newPrice', function(data){
   var newPrice = data.value  
});

socket.on('oldPrice', function(data){
   var oldPrice = data.value    
});

Open in new window


I would like to add this:

if (newPrice < oldPrice) {

            }else if (newPrice > oldPrice) {

            }

Open in new window


Thanks!
0
Comment
Question by:hallowz
1 Comment
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 total points
ID: 39718633
Hi,
I think you have to generate the variables "globally" like
var socket = io.connect('http://localhost:1000');
var newPrice = 0, oldPrice = 0;

socket.on('newPrice', function(data){
   newPrice = data.value  
});

socket.on('oldPrice', function(data){
   oldPrice = data.value    
});

Open in new window


Your compare should then be like
if (newPrice < oldPrice) {

            }else {

            }

Open in new window


HTH
Rainer
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to dynamically set the form action using jQuery.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

808 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