JavaScript (jquery) code _javascript tips for modifying global variables using functions

Source: Internet
Author: User
Tags system log
I am storing the current page with a global variable. In the last page link, click Method to call a function to automatically modify the number of pages to the last page, and then invoke the method that contains the comment.
But I found that click on the "last page" the first time the system did not respond, and then click once to grab the last page of information!
To say that the global variable has not changed, it should be changed, as if the number of pages and the method of loading comments failed.
The code is as follows:
Copy Code code as follows:

var page = 1; Initialize pages as first page
var str = "";
$ (document). Ready (function () {
Lostguest (); How to load comments
$ ("A#first"). Click (function () {
page = 1;
Lostguest ();
});
$ ("A#last"). Click (function () {
if (page > 1) {
--page;
Lostguest ();
}
else {
page = 1;
Alert ("It's already the first page!") ")
}
})
$ ("A#next"). Click (function () {
if (Page < PageCount) {
++page;
Lostguest ();
}
else {
Alert ("It's already the last page!") ");
}
})
$ ("A#all"). Click (function () {
Getpagecount ();
Lostguest ();
});
})
var getpagecount = function () {
var type = "Getpagecount";
$.ajax ({
URL: ' getcount.ashx?type= ' + type,
Type: "Get",
DataType: ' Text ',
Beforesend:function () {
},
Error:function () {
Alert (' Get the number of system log records failed ');
},
Success:function (count) {
Pages = number (count);
}
})
}
var lostguest = function () {
$.ajax ({
URL: ' sqlhelper.ashx?page= ' + page,
Type: "Get",
DataType: ' JSON ',
Beforesend:function () {
$ ("#loading"). Show ();
},
Error:function () {
Alert (' Get system log failed ');
},
Success:function (msg) {
$ ("#guest"). empty ();
if (msg!= "0") {
var data = Msg.log;
str = "";
$.each (data, function (I, n) {
STR + + "<p id= ' Xuhao ' > Serial number:" + N. Serial number + "Publication date" + N. Date + "Username:" + N. operator + "</p>";
STR + + "<p id= ' content ' > Contents:" + N. Event + "</p>";
});
$ ("#guest"). Append (str);
$ ("#loading"). Hide ();
}
else {
Alert ("0");
}
}
})
}

I don't know what's going on. Can you explain??
PS: Later found an alternative method to achieve this effect. The code is as follows:
Copy Code code as follows:

var page = 1;
var str = "";
var PageCount; Storage Total Pages
$ (document). Ready (function () {
Getpagecount (); How to get the total number of pages
Lostguest ();
$ ("A#first"). Click (function () {
page = 1;
Lostguest ();
});
$ ("A#last"). Click (function () {
if (page > 1) {
--page;
Lostguest ();
}
else {
page = 1;
Alert ("It's already the first page!") ")
}
})
$ ("A#next"). Click (function () {
if (Page < PageCount) {
++page;
Lostguest ();
}
else {
Alert ("It's already the last page!") ");
}
})
$ ("A#all"). Click (function () {
page = PageCount; Update current pages to total pages
Lostguest ();
});
})
var getpagecount = function () {
var type = "Getpagecount";
$.ajax ({
URL: ' getcount.ashx?type= ' + type,
Type: "Get",
DataType: ' Text ',
Beforesend:function () {
},
Error:function () {
Alert (' Get the number of system log records failed ');
},
Success:function (count) {
PageCount = number (count); Read Total pages
}
})
}
var lostguest = function () {
$.ajax ({
URL: ' sqlhelper.ashx?page= ' + page,
Type: "Get",
DataType: ' JSON ',
Beforesend:function () {
$ ("#loading"). Show ();
},
Error:function () {
Alert (' Get system log failed ');
},
Success:function (msg) {
$ ("#guest"). empty ();
if (msg!= "0") {
var data = Msg.log;
str = "";
$.each (data, function (I, n) {
STR + + "<p id= ' Xuhao ' > Serial number:" + N. Serial number + "Publication date" + N. Date + "Username:" + N. operator + "</p>";
STR + + "<p id= ' content ' > Contents:" + N. Event + "</p>";
});
$ ("#guest"). Append (str);
$ ("#loading"). Hide ();
}
else {
Alert ("0");
}
}
})
}

The first code problem can you explain?? Thank you!
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.