Javascript中的线程安全? [英] Thread Safety in Javascript?

查看:247
本文介绍了Javascript中的线程安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为save()的函数,此函数收集页面上的所有输入,并对服务器执行AJAX调用以保存用户工作的状态。

I have a function called save(), this function gathers up all the inputs on the page, and performs an AJAX call to the server to save the state of the user's work.

save()当用户单击保存按钮时执行,或执行一些其他操作,这需要我们在服务器上生成最新状态(例如从页面生成文档)。

save() is currently called when a user clicks the save button, or performs some other action which requires us to have the most current state on the server (generate a document from the page for example).

我添加了能够自动保存用户的工作每隔一段时间。首先,我想防止自动保存和用户生成的保存同时运行。所以我们有下面的代码(我正在削减大部分的代码,这不是一个1:1,但应该足以得到的想法横跨):

I am adding in the ability to auto save the user's work every so often. First I would like to prevent an AutoSave and a User generated save from running at the same time. So we have the following code (I am cutting most of the code and this is not a 1:1 but should be enough to get the idea across):

var isSaving=false;
var timeoutId;
var timeoutInterval=300000;
function save(showMsg)
{
  //Don't save if we are already saving.
  if (isSaving)
  { 
     return;
  }
  isSaving=true;
  //disables the autoSave timer so if we are saving via some other method
  //we won't kick off the timer.
  disableAutoSave();

  if (showMsg) { //show a saving popup}
  params=CollectParams();
  PerformCallBack(params,endSave,endSaveError);

}
function endSave()
{  
    isSaving=false;
    //hides popup if it's visible

    //Turns auto saving back on so we save x milliseconds after the last save.
    enableAutoSave();

} 
function endSaveError()
{
   alert("Ooops");
   endSave();
}
function enableAutoSave()
{
    timeoutId=setTimeOut(function(){save(false);},timeoutInterval);
}
function disableAutoSave()
{
    cancelTimeOut(timeoutId);
}

我的问题是这个代码是否安全?主要的浏览器是否只允许一个线程一次执行?

My question is if this code is safe? Do the major browsers allow only a single thread to execute at a time?

一个想法是,用户点击保存并得到没有响应会更糟,因为我们是自动保存(我知道如何修改代码来处理这个)。有人在这里看到任何其他问题吗?

One thought I had is it would be worse for the user to click save and get no response because we are autosaving (And I know how to modify the code to handle this). Anyone see any other issues here?

推荐答案

浏览器中的JavaScript是单线程的。你只能在任何时间点在一个功能。函数将在输入下一个函数之前完成。你可以指望这种行为,所以如果你在 save()函数,你将永远不会再输入,直到当前一个完成。

JavaScript in browsers is single threaded. You will only ever be in one function at any point in time. Functions will complete before the next one is entered. You can count on this behavior, so if you are in your save() function, you will never enter it again until the current one has finished.

这里有时会引起混淆(但仍然是真的)是当你有异步服务器请求(或setTimeouts或setIntervals),因为那样感觉你的函数是 interleaved

Where this sometimes gets confusing (and yet remains true) is when you have asynchronous server requests (or setTimeouts or setIntervals), because then it feels like your functions are being interleaved. They're not.

在您的情况下,两个 save()调用不会相互重叠,自动保存和用户保存可以背靠背发生。

In your case, while two save() calls will not overlap each other, your auto-save and user save could occur back-to-back.

如果您只想保存至少每x秒发生一次,您可以对自己的setInterval保存功能并忘记它。我不认为需要 isSaving 标志。

If you just want a save to happen at least every x seconds, you can do a setInterval on your save function and forget about it. I don't see a need for the isSaving flag.

我认为你的代码可以被简化很多:

I think your code could be simplified a lot:

var intervalTime = 300000;
var intervalId = setInterval("save('my message')", intervalTime);
function save(showMsg)
{
  if (showMsg) { //show a saving popup}
  params=CollectParams();
  PerformCallBack(params, endSave, endSaveError);

  // You could even reset your interval now that you know we just saved.
  // Of course, you'll need to know it was a successful save.
  // Doing this will prevent the user clicking save only to have another
  // save bump them in the face right away because an interval comes up.
  clearInterval(intervalId);
  intervalId = setInterval("save('my message')", intervalTime);
}

function endSave()
{
    // no need for this method
    alert("I'm done saving!");
}

function endSaveError()
{
   alert("Ooops");
   endSave();
}

这篇关于Javascript中的线程安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆