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

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

问题描述

我有一个叫保存()函数,这个函数在页面上收集了所有的输入,并执行一个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() 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).

我添加到自动保存用户的工作,每隔一段时间的能力。首先,我想prevent的自动保存和用户生成的,从在同一时间运行保存。因此,我们有以下的code(我割大部分code的,这是不是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);
}

我的问题是,如果这个code是安全的?这样做主要的浏览器只允许一个线程同时执行?

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

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

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的浏览器中是单线程的。你将永远只能是在一个函数在任意时间点。进入下一个功能之前将完成。你可以指望这种行为,因此,如果你在你的保存()功能,你将永远不会再次,直到当前已完成输入。

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.

如果这有时会混淆(然而仍是如此)是当你有异步服务器请求(或一个定时器,或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.

在你的情况,而两个保存()调用不会互相重叠,您自动保存和用户保存可能会出现背到了。

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.

我觉得你的code可以简化很多:

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