Javascript /节点和时间

时间:2016-09-20 15:29:06

标签: javascript angularjs node.js datetime time

让我们说用户正在点击一个按钮,时间就是,假设为0

首先要处理请求服务器端,就是再次花时间,这次也说它为0,存储在名为currenttime的var中(带new Date().getTime()

然后服务器正在运行,事情需要时间,直到它调用一个函数来存储时间,并给用户一些等待时间。

问题是,假设我将当前时间变量发送到函数并保存,并添加让我们说90分钟(1 1/2小时)。

然后客户端,我检索用户需要等待的时间。 但在这种情况下,结果是90分钟(有些exstra秒,大约15-30秒)。

从发送请求到服务器,我怎样才能确保用户不需要等待超过90分钟?

函数调用:

// after processing things this occurs
    userData.updateTimers(userid, whattotime, 5400,currenttime);

5400是5400秒,90分钟。 currenttime是new Date().getTime()在查询中首先检索的变量。

语言功能:

function updateTimers(userid,type,sec,time) {
    return new Promise( function (resolve, reject) {
        var newtime = time + (sec * 1000);

        var updateObj = {};
        updateObj[type] = newtime;

        usertimers.update({userid: userid},{$set: updateObj}).then(function (result) {
            console.log("updated timers");
            console.log(result);
            return resolve(result);
        })
    });
}

再次,我怎样才能确保它的90分钟,而不是包括处理时间? 我需要准确更新计时器。

我使用AngularJS前端的平均堆栈。将时间花在那里并将其置于服务器端以备将来使用,或者它能做得更好吗?

是一种解决方法吗?

因为无论我做什么服务器支持并添加一些等待时间,它总是会多几秒钟:/

2 个答案:

答案 0 :(得分:0)

使用勾选的@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) { View rootView = inflater.inflate(R.layout.fragment_blank, container, false); return rootView; } else if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) { View rootView = inflater.inflate(R.layout.fragment_sub_page02, container, false); Button mButton = (Button) rootView.findViewById(R.id.button); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // here you set what you want to do when user clicks your button, } }); return rootView; } else { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } } 并测试日期偏移。

然后您可以发送"完成"的日期(watchdog)给用户。



new Date().getTime() + (1000 * 60 * 90)




答案 1 :(得分:0)

如果我了解您的问题,您可以使用类似Promise.timeout的内容:

Promise.timeout = function(timeout, promise){
  return Promise.race([
  promise,
  new Promise(function(resolve, reject){
    setTimeout(function() { reject('Timed out'); }, timeout);
  })
]);
}

您可以这样使用它:

Promise.timeout(yourRequestFunctionWhenUserClicksButton(), 1000 * 90); //90 seconds

基本上,如果请求持续超过90秒,它将返回被拒绝的承诺,否则它将满足服务器应答的值。

相关问题