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

查看:47
本文介绍了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)时,这有时会令人困惑(但仍然如此),因为那时感觉您的函数正在交错.他们不是.

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天全站免登陆