简体   繁体   English

防止在页面刷新后重置JavaScript代码并重新开始

[英]Prevent javascript code to be reset and start over after page refresh

I have a rails app where in my application.js I have a setInterval() and inside of it an AJAX call that send a post request every minute to my controller in order to perform a create action. 我有一个Rails应用程序,在我的application.js文件中有一个setInterval() ,其中有一个AJAX调用,该调用每分钟向我的控制器发送一个发布请求,以执行创建操作。

setInterval(function(){

        $.ajax({
          type: "POST",
          url: "/post",
          data: { parameter: value },
          success: function (data) {
             //some logic
           }       
        });

    }, 60000);

My problem is that if the client refresh its page every 30 sec (for exemple) the setInterval() set for 1 minute will never be triggered. 我的问题是,如果客户端每隔30秒刷新一次页面(例如),设置为1分钟的setInterval()将永远不会触发。

Is there a way to make my javascript code not dependent of any page refresh so that if different users arrive at different time they get to see the same thing. 有没有一种方法可以使我的javascript代码不依赖于任何页面刷新,因此,如果不同的用户在不同的时间到达,他们将看到同一件事。 I guess it has to do with cookies or local storage but I have no idea how to implement that in a reliable way. 我想这与Cookie或本地存储有关,但我不知道如何以可靠的方式实现它。

In other word, I would like my js code to be run server side without being disrupted by page refreshes or client request which keep reseting my code. 换句话说,我希望我的js代码在服务器端运行,而不会由于页面刷新或客户端请求(这些代码不断重置我的代码)而中断。

Thank you for your guidance. 感谢您的指导。

You can use https://github.com/javan/whenever 您可以随时使用https://github.com/javan/

every 1.minute do # 1.minute 1.day 1.week 1.month 1.year is also supported
  runner "MyModel.some_process"
  rake "my:rake:task"
  command "/usr/bin/my_great_command"
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM