A small test found can automatically do the problem, so you want to see through the script way to get the corresponding question bank, just can learn JS asynchronous operation. Spent a day, finally ran smoothly, met a lot of pits. Record it and share it.
1, JS how to execute sequentially
JS has a powerful asynchronous operation mechanism, but because the idea of sequential execution is limited, the order of the request results will be messed up by using the for loop directly. Therefore, JS can be executed sequentially on the basis of asynchronous, and the basic method is to use callback. At the same time, in order to avoid JS in the DOM of the page during the operation of the DOM does not exist when the use of settimeout to delay execution (this place cost a small half a day).
Note that the $.ajax request is used in the program code, so the statement of the callback is placed in the success method of the $ajax request.
The basic code structure code is as follows:
var i = 0;var Loopcall = function (count) { callprocess (i, function () { i++; if (I < count) { Loopcall (count); }} );}; function callprocess (I, callback) { Console.log (' running on: ' + i.tostring ()); Process code //... //... Use SetTimeout delay to avoid DOM operation error setTimeout (function () {callback ();},100);} Loopcall (1000);
2. How the data is stored
Previously only know that there is a localstorage, the development process Google to know that there are websql, syntax and operation is quite simple, but also tried to use array variables to store data, but found that the variable data is more than 150K or so when the browser is very slow. So use Websql to store
var db = OpenDatabase (' MyDatabase ', ' 2.0 ', ' My db ', 2 * 1024x768); Db.transaction (Function (TX) { tx.executesql (' CREATE TABLE IF not EXISTS T1 (id unique, log) '); Tx.executesql (' INSERT into T1 (id,log) VALUES (?,? '), [e_id, E_log]; E_ID and E_log are external variables
Summarize
Other processing code is basically the original page code copy to modify the changes plus a certain debugging. Before want to use JS to write has not found the scene, now used although encountered some problems but finally overcame. However, JS will need to be well planned after the asynchronous invocation of multi-layered nesting to be processed as synchronous, although the promise type is added after ES6.
Crawl data directly in chrome