我的 JavaScript 代码不是线性运行的
My JavaScript code is not being run linearly?
我对JavaScript相当陌生,我一直在尝试创建一个Chrome扩展程序。Chrome扩展程序基本上搜索特定的书签"集"并将它们添加到数组中。我认为用代码更容易解释:
var books = [];
function getBookmarks(){
console.log("Bookmarks!");
chrome.bookmarks.getTree(function(bookmarks){
search_for_title(bookmarks, "Music", null, books); //Collect all bookmarks in the "Music" folder and put them into the books array
});
for(var i = 0; i < books.length; i++){ //Once all bookmarks are added to the books array, loop through all of them
console.log("ITEM: " + books[i]);
}
console.log("books: " + books.length); //Print out the length of the books array
}
function search_for_title(bookmarks, title, parent){
if(parent == null){ //First find the parent folder
for(var i = 0; i < bookmarks.length; i++){ //Loop through all bookmarks
if(bookmarks[i].title == title){ //If the bookmark title matches the title of the folder we're looking for ("Music"), proceed
search_for_title(bookmarks[i].children, null, bookmarks[i].id); //Loop through all the bookmarks in the folder that we found
console.log("books length: " + books.length);
} else{
if(bookmarks[i].children){ //If the bookmark is a folder, it has children
search_for_title(bookmarks[i].children, title, parent);
}
}
}
} else if(title == null){ //Parent folder is found, now just traverse the bookmarks within
for(var i = 0; i < bookmarks.length; i++){
console.log("books[" + i + "] = " + bookmarks[i].title + " , books length: " + books.length);
books[i] = bookmarks[i].title; //Assign all the bookmarks into the books array
}
}
}
getBookmarks();
console.log("FINAL BOOKS: " + books.length);
需要注意的几件事:var books = [] 在全局范围内声明和初始化,函数search_for_title递归,并且在此代码片段的末尾调用 getBookmarks() 以启动代码。
现在,我通常来自Java和C++背景,那里的所有内容都是线性的,并且没有这些"回调"函数是我不熟悉的。所以,我认为输出应该是:
书签!
- 打印出"音乐"文件夹中的所有书签,然后返回getBookmarks功能-
项目:。。。
项目:。。。
项目:。。。
。
书籍:625 **(书籍的长度)**
最终书籍: 625
但是,我得到的输出是:
书签!这很好
书籍: 0 为什么在开头是这样?此值应为 625
最终书籍: 0 为什么程序中的最后一行代码几乎在开头?此值应为 625
- 找到的所有书签 - 这很好
书籍长度:0 这应该靠近程序的开头,如第二行正确的值为 0
我显然是在 search_for_title 方法中直接访问全局"books"数组,但由于某种原因,它总是告诉我数组的长度为 0。此外,由于某种原因,我的代码似乎没有任何线性路径,它只是到处跳跃。此外,当程序打印出每个单独的书签时,它会打印出书籍数组的正确长度(即书籍[599] = Nero - Promise - YouTube,书籍长度:600),但是为什么当它返回getBookmarks函数时,它总是说长度为0?
JavaScript 是线性运行的,但是chrome.bookmarks.getTree
方法是异步调用。这意味着 #getTree 方法之后的代码不会等到方法完成。
在JavaScript中,函数是第一类对象。回调函数被传递到 #getTree 方法中,该方法在执行自己的逻辑后执行参数函数。
在当前代码中,每次的日志结果都会略有不同,因为每个异步调用需要不同的时间才能完成。您应该能够通过简单地在回调函数中移动循环和日志代码来完成这项工作。
function getBookmarks(){
console.log("Bookmarks!");
chrome.bookmarks.getTree(function(bookmarks){
search_for_title(bookmarks, "Music", null, books); //Collect all bookmarks in the "Music" folder and put them into the books array
for(var i = 0; i < books.length; i++){ //Once all bookmarks are added to the books array, loop through all of them
console.log("ITEM: " + books[i]);
}
console.log("books: " + books.length); //Print out the length of the books array
});
}
- 如何使用phaser使html5游戏在移动设备浏览器上运行
- 使用压缩的JavaScript文件(不是运行时压缩)
- Javascript运行php文件,然后下载文件
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- 我已经创建了一个jquery转盘,并使用if条件来运行和停止转盘
- Angularjs代码未在匿名函数中运行
- jquery设置为使用参数运行
- 如何根据时间运行不同的脚本
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- 将文本框链接到由按钮运行的javascript公式
- 为什么不是't运行此Javascript的Chrome
- ng应用程序使脚本无限运行
- 如何在运行时在angular 2中加载外部js脚本
- 与运行长作业(javascript,node.js)的第三方API同步的最佳实践
- JavaScript错误:Microsoft JScript运行时错误:应为对象
- 在终端中运行 JavaScript 时(使用 rhino),如何使用 print() 函数在一行中打印
- Highcharts colorsByPoint与系列中的线性渐变一起使用时不起作用
- 如何使用JS/nightwatchjs并行运行多个测试
- 茉莉花宝石-耙茉莉花:ci dons't运行测试
- 我的 JavaScript 代码不是线性运行的