elliott.david 2018-04-16 16:57 采纳率: 25%
浏览 15

跳过json对象(PHP会话)

I have an array of users. Each user can be skipped on .click button. However I want it to skip the user that logs in. The session is started with PHP, but I'm showing the user through ajax and javascript. However

       if(sSession = sKey){
            aPeople.slice(this.jPerson);
        }

is not correctly skipping the user. Could you help?

Here's the code:

$("#btnSkip").click(function() {
$.getJSON('include/users.txt', function(aPeople){

        var getPerson = function(id) {
            var jPerson = aPeople[id]
            var sID = jPerson.id
            var sName = jPerson.name
            var sImage = jPerson.picture                
            var sSession = $('#mySession').text()
            var sKey = jPerson.key

            //this if statement doesn't work
            if(sSession == sKey){
                console.log(sSession)
                console.log(sKey)
                console.log(personIndex)
                console.log(jPerson)
                aPeople.splice(jPerson);
            }

            $('#sName').text(sName)
            $('#sImg').attr('src', sImage)

            //TO START COUNT AGAIN

            if(aPeople.length -1 == personIndex){
                personIndex = 0
            }else{
                personIndex = personIndex + 1
            }    
        }
            getPerson(personIndex);

            $( '#sName' ).fadeIn( 'slow' )
            $( '#sImg' ).fadeIn( 'slow' )
            })
        })
  • 写回答

1条回答 默认 最新

  • weixin_33730836 2018-04-17 04:44
    关注

    So, it looks to me like to you want to:

    1. Retrieve an array of user objects.
    2. Navigate the retrieved list and display each user on a button click.
    3. Never display the logged in user.

    Here's a suggested solution:

    1. I'm filtering out the logged in user with the jQuery grep function.
    2. I simplified your navigation logic.
    3. I wasn't sure you meant to retrieve the users JSON on every click, so I altered that also.

      function setupUserNavigator(users, loggedInUserKey) {
        var idx = 0;
    
        // filter out the logged in user
        var filtered = $.grep(users, function(user) {
          return !(user.key === loggedInUserKey);
        });
    
        function current() {
          return filtered[idx];
        }
    
        function next() {
          idx += 1;
        }
    
        function more() {
          return idx < filtered.length - 1;
        }
    
        return { current, next, more };
      }
    
      function displayUser(user) {
        $('#sName').text(user.name);
        $('#sImg').attr('src', user.picture);
      }
    
      function usersResponseHandler(users) {
        var loggedInUserKey = $('#mySession').text();
        var userNavigator = setupUserNavigator(users, loggedInUserKey);
    
        // display the first user immediately
        displayUser(userNavigator.current());
    
        // display each subsequent user on a 'next' button click
        $('#next').click(function() {
          userNavigator.next();
          displayUser(userNavigator.current());
    
          if(!userNavigator.more()) {
            $(this).prop('disabled', true);
          }
        });
      }
      
      // $.getJSON('include/users.txt', usersResponseHandler);
    
      // use test data for the snippet and mock response handler call
      var data = [
        { id: '1', key: '1234', name: 'Joe', picture: 'img/joe.png' },
        { id: '2', key: '5678', name: 'John', picture: 'img/john.png' },
        { id: '3', key: '9012', name: 'Sarah', picture: 'img/sarah.png' },
        { id: '4', key: '0987', name: 'Tim', picture: 'img/tim.png' },
        { id: '5', key: '6543', name: 'Lily', picture: 'img/lily.png' }
      ];
      
      usersResponseHandler(data);
      
      
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    Logged In User Key:
    <div id="mySession">9012</div><br />
    
    Name:
    <div id="sName"></div>
    Picture:
    <div><img id="sImg" src="" /></div><br />
    <button id="next">Next</button>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助