Home Web Front-end JS Tutorial JavaScript (jquery) uses functions to modify the code of global variables_javascript skills

JavaScript (jquery) uses functions to modify the code of global variables_javascript skills

May 16, 2016 pm 06:42 PM
global variables function

I store the current page in a global variable. At the same time, click the method in the "Last Page" link to call a function to automatically change the page number to the last page, and then call the method to load comments.
But I found that the system did not respond when I clicked "Last Page" for the first time, and the information on the last page was captured when I clicked it again!
To say that the global variable has not changed, it should have been changed, just like the method of loading comments becomes invalid after changing the page number.
The code is as follows:

Copy code The code is as follows:

var page = 1; / /Initialize the page number to the first page
var str = "";
$(document).ready(function() {
lostguest(); //Method to load comments
$( "a#first").click(function() {
page = 1;
lostguest();
});
$("a#last").click(function() {
if (page > 1) {
--page;
lostguest();
}
else {
page = 1;
alert("Already The first page! ")
}
})
$("a#next").click(function() {
if (page < pagecount) {
page;
lostguest();
}
else {
alert("It’s 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('Failed to obtain the number of system log records');
},
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('Failed to obtain system log');
},
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"Published date" n.Date"Username:" n.Operator"</p>";
str = "<p id='content'>Content:" n.Event"</p>";
});
$("#guest").append(str);
$("#loading").hide();
}
else {
alert("0");
}
}
})
}

I don’t know what’s going on? Can you experts explain it? ?
PS: Later I found a workaround to achieve this effect. The code is as follows:
Copy code The code is as follows:

var page = 1;
var str = "";
var pagecount; //Storage the total number of pages
$(document).ready(function() {
getpagecount(); //Method 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("Already the first page!")
}
})
$("a#next").click(function() {
if (page < pagecount) {
page;
lostguest();
}
else {
alert("It’s the last page!");
}
})
$("a#all").click(function() {
page = pagecount; //Update the current page number to the total number of pages
lostguest();
});
})
var getpagecount = function() {
var type = "GetPageCount";
$.ajax({
url: 'GetCount.ashx?type=' type,
type: "GET",
dataType: 'text',
beforeSend: function() {
},
error: function() {
alert('Failed to obtain the number of system log records') ;
},
success: function(count) {
pagecount = Number(count); //Total number of pages read
}
})
}
var lostguest = function() {
$.ajax({
url: 'SqlHelper.ashx?page=' page,
type: "GET",
dataType: 'json',
beforeSend: function() {
$("#loading").show();
},
error: function() {
alert('Failed to obtain system log');
},
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'>Content: "n.Event"</p>" ;
});
$("#guest").append(str);
$("#loading").hide();
}
else {
alert("0");
}
}
})
}

Can any experts explain the problem with the first code? ? Thanks!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tips for dynamically creating new functions in golang functions Tips for dynamically creating new functions in golang functions Apr 25, 2024 pm 02:39 PM

Tips for dynamically creating new functions in golang functions

Considerations for parameter order in C++ function naming Considerations for parameter order in C++ function naming Apr 24, 2024 pm 04:21 PM

Considerations for parameter order in C++ function naming

How to write efficient and maintainable functions in Java? How to write efficient and maintainable functions in Java? Apr 24, 2024 am 11:33 AM

How to write efficient and maintainable functions in Java?

Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Apr 21, 2024 am 10:21 AM

Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters

Complete collection of excel function formulas Complete collection of excel function formulas May 07, 2024 pm 12:04 PM

Complete collection of excel function formulas

What are the benefits of C++ functions returning reference types? What are the benefits of C++ functions returning reference types? Apr 20, 2024 pm 09:12 PM

What are the benefits of C++ functions returning reference types?

Advanced usage of reference parameters and pointer parameters in C++ functions Advanced usage of reference parameters and pointer parameters in C++ functions Apr 21, 2024 am 09:39 AM

Advanced usage of reference parameters and pointer parameters in C++ functions

C++ Function Exception Advanced: Customized Error Handling C++ Function Exception Advanced: Customized Error Handling May 01, 2024 pm 06:39 PM

C++ Function Exception Advanced: Customized Error Handling

See all articles