函数在执行下一行之前是否不等待行执行完成
我习惯了用 python 编程,并且正在尝试学习 javascript。我希望该应用程序能够在 Chrome 上运行,所以不关心使其跨浏览器。我也想在不使用 jquery 或其他库的情况下编写它 - 因为我刚刚学习 javascript。
我的普遍问题是,函数调用通常看起来像是“跳过”的。我正在使用 firebug,在控制台上没有看到任何错误。因此,如果我在一个函数中有两个调用,
mydict_user_values = return_dict_of_user_names()
alert("I HAVE USER VALUES " + mydict_user_values.length)
// This next line gets called sometimes before the previous line so the values in the table filled in by next line are blank even though the dictionary above has the correct length
fill_user_name_values_into_table(mydict_user_values)
我习惯了 python,不明白发生了什么。抱歉问了一个抽象的问题,但是我没有得到关于 javascript 的一些基本知识。
编辑:一天-6点。我很震惊!我知道我的代码中存在一些严重的新手错误:我已将其放在所有新手错误中。
https://github.com/harijay/gridzilla_web
我的典型函数:
function return_dict_of_user_names()
{
var my_new_dict
var my_user_names = document.getElementsByClassName("my_users");
for (var i = 0; i <= my_user_names.length ; i++){
var a_num = document.getElementById("user_number_" + i).value
var a_name = document.getElementById("user_name_" + i).value
my_new_dict[i] = [a_num,a_name]
}
return my_new_dict
}
然后我还有其他 javascript 函数这些值并填充同一页面的另一部分。
function fill_user_names_into_div(mydict){
var my_username_dict = mydict;
//Javascript code to fill in the values by lines like
my_fillable_elements = document.getElementsByClassName("user_table_fancy_entry")
for (var i = 0 ; i <= my_fillable_elements.length ; i++){
document.getElementById("usertable_user_number_" + i).value = my_username_dict[1][1]
}
I am used to programming in python and am trying to learn javascript. I want the application to work on chrome so dont care to make it cross-browser. I also want to write it without using jquery or another library - since I am just learning javascript.
My general problem is that very often the function calls seem like they are "skipped". I am using firebug and dont see any errors on the console. So If I have two calls calls right after each other in a function
mydict_user_values = return_dict_of_user_names()
alert("I HAVE USER VALUES " + mydict_user_values.length)
// This next line gets called sometimes before the previous line so the values in the table filled in by next line are blank even though the dictionary above has the correct length
fill_user_name_values_into_table(mydict_user_values)
I am used to python and dont understand whats going on. Sorry for an abstract question , but there is something fundamental about javascript that I am not getting.
edited: -6 points in one day. I am quite shocked!. I know there are some serious newbie errors in my code: Which I have put here in all its newbie badness.
https://github.com/harijay/gridzilla_web
My Typical functions:
function return_dict_of_user_names()
{
var my_new_dict
var my_user_names = document.getElementsByClassName("my_users");
for (var i = 0; i <= my_user_names.length ; i++){
var a_num = document.getElementById("user_number_" + i).value
var a_name = document.getElementById("user_name_" + i).value
my_new_dict[i] = [a_num,a_name]
}
return my_new_dict
}
I then have other javascript functions that take these values and populate another part of the same page.
function fill_user_names_into_div(mydict){
var my_username_dict = mydict;
//Javascript code to fill in the values by lines like
my_fillable_elements = document.getElementsByClassName("user_table_fancy_entry")
for (var i = 0 ; i <= my_fillable_elements.length ; i++){
document.getElementById("usertable_user_number_" + i).value = my_username_dict[1][1]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Javascript 语句是同步执行的。我认为你有一些 JavaScript 错误,可能会在某些地方停止执行。您应该查看 javascript 错误控制台或调试控制台以查看哪里存在 javascript 错误。例如,在此函数中:
在使用 my_new_dict 之前,您必须将其初始化为数组,如下所示:
另外,javascript 语句应以分号结尾,我建议使用
.push()
来将一个项目添加到数组的末尾。Javascript statements are executed synchronously. I think you have some javascript errors that is probably halting execution in some places. You should be looking in an javascript error console or a debug console to see where you have javascript errors. For example, in this function:
You have to initialize my_new_dict to an array before using it as such like this:
Also, javascript statements should end with a semicolon and I'd recommend using
.push()
to add an item onto the end of an array.