views:

117

answers:

4

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");
        });
    });
});
A: 

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.

Carson Myers
**Oh yes he has much bigger problems**, trust me.
Jacob Relkin
+3  A: 

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);
        });

});
Strelok
It's definetly the problem. After I looked at it again, it's nothing to do with jQtouch. Basically your `updateVerse()` function was calling `$("...").tap(function(){...});`. What that does it every time updateVerse is called *another* tap/click handler is attached to the hyperlink. In turn each handler that you were attaching was actually calling `updateVerse()` again. So with every click you were attaching +1 tap/click handler to your link. You must remember that when the handlers are attached to elements with jQuery they are cumulative, so the previous handlers do not get cleared.
Strelok
Oh wow. This is really only my second time using jQuery. It's so different from anything else... Hum, maybe I should find a book to read about it for some easy reading. Or just go read the jQuery Docs. I makes sense now, thanks so much! It seems like inheritance is something that is not well explained in any language, yet it seems to bite me every time.
michael
+1  A: 

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.

Joey Adams
This is my suspicion as well.
Alastair Pitts
A: 

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');  
        }  
    } 
michael