Structure of async callbacks in javascript: Synching the Asynch

I find the seperation of code between when using callbacks makes my code harder to understand and maintain. How do you handle the problem? Here are a couple of solutions I have come up with, using, as an exaple, asynch web service calls. Please let me know what you think, and and pros or cons that occur to you. via closures: sayHelloWithClosures: function () { //Do something first

在javascript中异步回调的结构:同步异步

我发现使用回调之间的代码分离会让我的代码更难理解和维护。 你如何处理这个问题? 以下是我提出的几个解决方案,它们使用异步Web服务调用。 请让我知道你的想法,以及发生在你身上的优点或缺点。 通过关闭: sayHelloWithClosures: function () { //Do something first // The following call's signature is: ServiceName(SuccessCallback, FailureCallback); TestBasicWebServices.SL.WebService1

Javascript method chaining

Didn't know how else to put this. Say I have a JavaScript method that makes some AJAX calls: function makeAJAXCalls() { // who knows how long this will take } And I don't want the next function to execute until all the AJAX calls are complete. I also don't want to put the next function call into the success callback for the AJAX call. Is there a way I can like chain these,

Javascript方法链接

不知道如何把这个。 假设我有一个JavaScript方法可以实现一些AJAX调用: function makeAJAXCalls() { // who knows how long this will take } 我不希望下一个函数在所有的AJAX调用完成之前执行。 我也不想将下一个函数调用放入AJAX调用的成功回调函数中。 有没有一种方法可以将这些链接起来,所以直到makeAJAXCalls()完成它需要做的所有事情之后,下一个函数才会被调用? var queueCount =0; function makeAjaxCalls

Simulating context switches in JavaScript?

I've been working on implementing a pretty complex system in JavaScript that needs to simulate, among other things, multithreaded processes. In a real multithreaded process (such as a kernel thread) it's possible to switch between threads by context-switching. This works because you can store the current process's program counter and registers to a temporary structure, restore the p

模拟JavaScript中的上下文切换?

我一直在努力在JavaScript中实现一个非常复杂的系统,该系统需要模拟多线程进程等。 在真正的多线程进程中(例如内核线程),可以通过上下文切换在线程间切换。 这是可行的,因为您可以将当前进程的程序计数器存储并注册到临时结构,恢复程序计数器并注册其他进程,然后恢复之前进程中离开的地方。 我很好奇在JavaScript中是否可能有类似的东西。 我目前无法做到这一点,所以一直在使用协同式多任务设计系统。 特别是,我

How do I bootstrap a collection in Backbone.js using Javascript only

Context: I am building an application that needs several large collections of reference data to operation. I am limited to HTML and Javascript only (including JSON). Question : How do I bootstrap a collection in Backbone.js where the collection objects are in JSON format on the server and I'm using Javascript only? This is what I know already: Backbone.js bootstrapping best practice re

我如何在仅使用Javascript的Backbone.js中引导集合

上下文:我正在构建一个需要几个大型参考数据集合进行操作的应用程序。 我仅限于HTML和Javascript(包括JSON)。 问题 :如何在Backbone.js中引导集合中服务器上的集合对象为JSON格式的集合,并且仅使用Javascript? 这是我所知道的: Backbone.js引导最佳实践需要Rails或其他服务器端语言(http://backbonejs.org/#FAQ-bootstrap)。 大多数Javascript I / 0操作都是异步的,比如从服务器加载JSON。 使用fetch()引

Force code to execute after another method finishes executing

Here is what I want to do: setSource is a function which executes about 3 seconds. editor.setSource(); setTimeout(function () { //do something, some commands }, 3000); I want the //do something, some commands part to be executed AFTER the last line of setSource() is executed. Now I'm doing it with setTimeout, but I think it's not very good solution, because sometimes setSou

强制代码在另一个方法完成执行后执行

这是我想要做的: setSource是一个执行大约3秒的函数。 editor.setSource(); setTimeout(function () { //do something, some commands }, 3000); 我想//执行一些操作,在执行setSource()的最后一行之后执行一些命令部分。 现在我用setTimeout来做这件事,但我认为这不是一个很好的解决方案,因为有时setSource()需要5秒钟才能执行。 这个怎么做? 让setSource接受一个回调参数: editor.setSource = func

How to handle multiple forms in one page

I am working one a page like the below <div id="first_div"> <form name='invoice_edit' id='invoice_edit' action='forms_invoice.php' method='post'> <table border="0"> <tr> <td id=customers_title_td >Customer</td> <td id=customers_td > <!-- php code for customer list //--> </td> </tr> &

如何在一个页面中处理多个表单

我正在像下面这样工作 <div id="first_div"> <form name='invoice_edit' id='invoice_edit' action='forms_invoice.php' method='post'> <table border="0"> <tr> <td id=customers_title_td >Customer</td> <td id=customers_td > <!-- php code for customer list //--> </td> </tr> </table>

How can I set the duration of this jQuery animation proportionally?

I've created a quick test to show what I'm trying to do: http://jsfiddle.net/zY3HH/ If you click the "Toggle Width" button once, a square will take one second to grow to full width. Click it again, and it will take one second to shrink down to zero width. However, click the "Toggle Width" button twice in rapid succession - the second time when the square has gro

我怎样才能按比例设置这个jQuery动画的持续时间?

我创建了一个快速测试来展示我想要做的事情: http://jsfiddle.net/zY3HH/ 如果单击“切换宽度”按钮一次,正方形将花费一秒时间增长至全宽。 再次单击它,它将需要一秒钟缩小到零宽度。 但是,快速连续点击两次“切换宽度”按钮 - 第二次当广场长度只增长到其总宽度的一小部分时(例如10%) - 您会注意到动画仍然需要一秒钟的时间才能完成返回广场零宽度,这看起来很尴尬,国际海事组织。 虽然这种行为是预期的,但我希望

dynamically load code and get line number of parse error

I have a tool, similar in ways to JSFiddle, that allows me to dynamically type in javascript and run it on a page. The code can be multiple lines, and typically will be. Unfortunately if there is an exception in the code I type in, I can't get the line number of the exception if I use eval() to run the code. I found a partial solution, which is instead of using try{ eval(code); } catc

动态加载代码并获取解析错误的行号

我有一个类似JSFiddle的工具,它允许我动态地输入javascript并在页面上运行它。 代码可以是多行,通常会是。 不幸的是,如果我输入的代码中存在异常,如果使用eval()运行代码,则无法获取异常的行号。 我找到了一个部分解决方案,而不是使用 try{ eval(code); } catch(e) { processException(e); } 改为做这样的事情: var s = document.createElement('script'); s.appendChild(document.createTextNode( "try

Is this a good pattern for my jQuery scripts?

I want to keep my scripts organized in one .js file for all my site (I have a mess right now), something like namespaces and classes in C#... (function ($) { //private variables $.divref = $("#divReference"); //Namespaces window.MySite = {}; window.MySite.Home = {}; window.MySite.Contact = {}; //Public function / method window.MySite.Home.Init = function(params

这是我的jQuery脚本的好模式吗?

我想把我的脚本组织在一个.js文件中,用于我所有的站点(我现在有一个混乱),就像C#中的命名空间和类一样。 (function ($) { //private variables $.divref = $("#divReference"); //Namespaces window.MySite = {}; window.MySite.Home = {}; window.MySite.Contact = {}; //Public function / method window.MySite.Home.Init = function(params){ alert("Init"); MySi

Algorithm to group messages by time

I need to sort messages in the following order: Received Today , Yesterday , and This Week Rules for This Week : starts at the most recent Monday - not including Today or Yesterday Meaning: If Today is Sunday, Yesterday is Saturday, This Week is Friday, Thursday, Wednesday, Tuesday and Monday If Today is Thursday, Yesterday is Wednesday, This Week is Tuesday and Monday But if Today is T

按时间分组消息的算法

我需要按以下顺序对消息进行排序: Today , Yesterday和This Week收到 This Week规则:从最近的星期一开始 - 不包括Today或Yesterday 含义: 如果Today是星期日, Yesterday是星期六, This Week是星期五,星期四,星期三,星期二和星期一 如果Today是星期四, Yesterday是星期三, This Week是星期二和星期一 但如果Today是星期二, Yesterday是星期一 - This Week没有日子 如果Today是星期一, Yesterday是星期天