开发者

jQuery i++ and i-- problems

Could someone please tell me what I am doing wrong? I'm not a newbie at programming but I feel like it tonight! Every time I increment the incrementing variable it throws a fit! When add one to it, it behaves fine, but if I try to add one more to it it wants to add 2 more. And then if I try to de-increment it wants to subtract from the original number that it was assigned to.

I've tried:

i++;
i = i+1;
i = i++;

Nothing seems to work. It's got to be a stupid mistake.

here's the code:

var dayNum = 30;
//----------------------------------------------------------------------
$.jQTouch({
        icon: 'dailyqoteicon.png',
        statusBar: false,
        initializeTouch: 'a.touch'
    });

//----------------------------------------------------------------------
$(document).ready(function(){
    //$(function(){});
    $(function(){
        $('a.touch').swipe( function(event, info){
            //alert("jQTouch swipe event"); 
            //alert(info.direction);
        });
    });
    $(function updateVerse(){
        //alert("updateVerse called");
        $.ajax({
            type: "GET",
            dataType: 'JSON',
            data: 'day='+ dayNum,
            url: 'forward.php',
            success: function(data){
                var obj = $.parseJSON(data);
                $("h2.quote").html("");
                $("h3.reference").html("");
                $("h2.quote").append(obj.quote);
                $("h3.reference").append(obj.reference, " ",  obj.version);
                //$("span.version").append(obj.version);
                //-----------------------------------
                // JSON string {"id":"1","quote":"For to me, to live is Christ, and to die is gain","reference":"Philippians 1:21","version":"NKJV"}
            },
            error: function(request, error){
                alert("problem retrieving json data string");
            }
        });
        function addDayNum开发者_开发问答(){
            dayNum = dayNum + 1;
            //dayNum = dayNum++;
        }
        function subDayNum(){
            dayNum = dayNum - 1;
            //dayNum = dayNum--;
        }

        $("div#header a.next").tap( function(){
            addDayNum();
            //dayNum++;// doesn't work at all
            //dayNum = dayNum + 1;//doesn't work at all
            updateVerse();
            //alert(dayNum);
            //alert("next clicked");
        });
        $("div#header a.prev").live('click', function(){
            subDayNum();
            //dayNum--;//doesn't work at all
            //dayNum = dayNum - 1;// doesn't work at all
            updateVerse();
            //alert(dayNum);
            //alert("previous clicked");
        });
    });
});


I am not too familiar with jqTouch. But what's happening is that a click handler is getting attached to the links every time the link is actually clicked. So with every click it's going to skip a verse N+1 times.

EDIT: I see it now. Your updateVerse function has this:

$("a.next").tap( function(){
addDayNum();// doesn't work at all
//dayNum++;// doesn't work at all
    //dayNum = dayNum + 1;//doesn't work at all
updateVerse();
//alert(dayNum);
//alert("next clicked");
});
$("a.prev").tap( function(){
subDayNum();// doesn't work at all
//dayNum--;//doesn't work at all
//dayNum = dayNum - 1;// doesn't work at all
updateVerse();
//alert(dayNum);
//alert("previous clicked");
});

This code should only run ONCE and what is happening it's adding tap() handler every time you actually tap a link. That's why it is skipping N+1 verses every time you tap a link.

Here is what your code should look like:

var dayNum = 30;

//----------------------------------------------------------------------
function setupClickHandlers() {
        $("div#header a.next").tap( function(){
            addDayNum();
            //dayNum++;// doesn't work at all
            //dayNum = dayNum + 1;//doesn't work at all
            updateVerse();
            //alert(dayNum);
            //alert("next clicked");
        });
        $("div#header a.prev").live('click', function(){
            subDayNum();
            //dayNum--;//doesn't work at all
            //dayNum = dayNum - 1;// doesn't work at all
            updateVerse();
            //alert(dayNum);
            //alert("previous clicked");
        });
}

function updateVerse(){
        //alert("updateVerse called");
        $.ajax({
            type: "GET",
            dataType: 'JSON',
            data: 'day='+ dayNum,
            url: 'forward.php',
            success: function(data){
                var obj = $.parseJSON(data);
                $("h2.quote").html("");
                $("h3.reference").html("");
                $("h2.quote").append(obj.quote);
                $("h3.reference").append(obj.reference, " ",  obj.version);
                //$("span.version").append(obj.version);
                //-----------------------------------
                // JSON string {"id":"1","quote":"For to me, to live is Christ, and to die is gain","reference":"Philippians 1:21","version":"NKJV"}
            },
            error: function(request, error){
                alert("problem retrieving json data string");
            }
        });
        function addDayNum(){
            dayNum = dayNum + 1;
            //dayNum = dayNum++;
        }
        function subDayNum(){
            dayNum = dayNum - 1;
            //dayNum = dayNum--;
        }

}

$(document).ready(function(){
        $.jQTouch({
            icon: 'dailyqoteicon.png',
            statusBar: false,
            initializeTouch: 'a.touch'
        });

    setupClickHandlers();
        $('a.touch').swipe( function(event, info){
            //alert("jQTouch swipe event"); 
            //alert(info.direction);
        });

});


Try alert()ing when the click handler is called. Beware that it might render your browser inoperable because of it popping up incessantly. There's nothing wrong with your attempts at ++ and -- (other than i = i++ being undefined behavior). In Chrome, it's visibly updating the verse multiple times and using 100% CPU. Perhaps the click handler is somehow invoking itself recursively.


You shouldn't do i = i++. In C this is undefined behavior, called modifying a variable twice with no sequence point in between -- although I don't think that applies here.

i++ will increment i and yield the original value. So,

i = 4
print i++ # prints 4
print i   # prints 5

therefore, if you say i = i++, you are saying, increment the value of i, then assign the original value to i. This is silly.

I suspect you may have deeper problems, but this is one thing that stuck out at me immediately.


I guess you can't post code in the comments... do you usually us pre tags or just the WYSIWYG?

@strelokstrelok sounds like that could be the problem. here's the actual tap() function in jQTouch. Is the parameter inside the function another function? What difference would that make?

        $.fn.tap = function(fn){  
        if ($.isFunction(fn)){  
            var tapEvent = (jQTSettings.useFastTouch && $.support.touch) ? 'tap' : 'click';  
            return $(this).live(tapEvent, fn);  
        } else {  
            $(this).trigger('tap');  
        }  
    } 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜