Problems with jQuery i ++ and i--

Can someone please tell me what I am doing wrong? I'm not new to programming, but today I like it! Every time I increment an incremental variable, it throws a fit! When you add one to it, it behaves fine, but if I try to add another, it will want to add another 2. And then, if I try to de-increment, it wants to subtract from the original number to which it was assigned.

I tried:

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

Nothing seems to work. It was a stupid mistake.

here is 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"); }); }); }); 
+4
source share
4 answers

I am not very familiar with jqTouch. But what happens is that the click handler binds to links every time a link is clicked. Thus, each click will skip the verse N + 1 times.

EDIT: I see it now. Your updateVerse function has the following:

 $("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 run only once and what happens with the addition of the tap() handler each time the link is clicked. That's why it skips N + 1 verses every time you click a link.

Here is what your code looks 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); }); }); 
+4
source

Try alert()ing when calling the click handler. Remember that this can cause your browser to become inoperative because it constantly appears. There is nothing wrong with your attempts in ++ and -- (except for i = i++ , which are undefined). In Chrome, it noticeably updates the verse several times and uses a 100% processor. Perhaps the click handler somehow calls recursively.

+1
source

You should not do i = i++ . In C, this behavior is undefined, called modifying a variable twice without an intermediate point between them - although I don't think this applies here.

i++ will increment i and display the original value. Thus,

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

therefore, if you say i = i++ , you say increase the value of i , and then assign the original value of i . This is stupid.

I suspect you might have deeper problems, but this is one thing that hit me right away.

0
source

I think you cannot post code in comments ... do you usually use pre-tags or just WYSIWYG?

@strelokstrelok seems like this could be a problem. here is the actual tap () function in jQTouch. Is a parameter inside a function another function? What's the difference?

  $.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
source

Source: https://habr.com/ru/post/1308432/


All Articles