1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to check if data from different functions generated by same user

Discussion in 'JavaScript' started by ketting00, Nov 26, 2014.

  1. #1
    ็Hi guys,

    I have a serious problem which I try to solve for 2 day straight now. Can you suggest how I can get the result I want from the mock-up code below. I've been asking around in several forums and no one can help me:
    var a = Math.floor((Math.random()*100)+1);
    var x = function() {
        var c = { d: a, e: 'klmn' },
            f = JSON.stringify({ type: 'test', data: c });
        return f;
    }
    var y = function() {
        var b = [a, 555];
        return b;
    }
    function callback(i) {
        var s;
        if (typeof i === 'string') {
             var t = i,
                u = JSON.parse(t);
                s = u.data.d;
        }
        if (typeof i === 'object') {
            var v = i,
                w = v[0];
                console.log(s); // --> problem is here
        }
    }
    var test = document.getElementById('test');
        test.addEventListener('click', function() {
            callback(x());
            callback(y());
        });
    Code (markup):
    What I'm doing:
    Please ignore the random number I just created it to represent the user ID. So every data is appended with the user ID. (I don't know why people pay attention only to this point.)

    In real life, or in my web app, the user's interact would generate two type of data: an array and a string, in separate functions. For string data I convert it to a JSON format and send it from a browser to a server along with array data in order to process them. The app at the server detects that string data always reaches it first (I don't know why), so I create a function to mock-up that data processing function on the same page because they do pretty much the same thing.

    My point is I want to match the ID to check if those data come from the same user before I process them. The problem is how do I check that.
    Note that I can't call the function as callback(x(),y());, which is working, because doing so data would send large data and it would cause Aw, Snap! on Chrome, or Stop Script! on other browsers.

    Thank you,
     
    Last edited: Nov 26, 2014
    ketting00, Nov 26, 2014 IP
  2. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #2
    Never mind. I solve it:
    
    var s, t, u;
    function callback(i) {
        if (typeof i === 'string') {
            return t = i;
        }
        if (typeof i === 'object') {
            u = i;
        }
        console.log(u);
    }
    
    Code (markup):
     
    ketting00, Nov 26, 2014 IP