Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在setIntervall()函数中停止跳转到页面顶部?(返回false或preventDefault?)_Javascript_Jquery_Ruby On Rails - Fatal编程技术网

Javascript 如何在setIntervall()函数中停止跳转到页面顶部?(返回false或preventDefault?)

Javascript 如何在setIntervall()函数中停止跳转到页面顶部?(返回false或preventDefault?),javascript,jquery,ruby-on-rails,Javascript,Jquery,Ruby On Rails,在我的rails应用程序中,我想每10秒用ajax调用一个控制器动作。这很好用。但是,每次执行代码时,页面都会跳回顶部,这是我不希望看到的 这是我目前的代码: $(document).ready(function(){ setInterval(function(){ $.ajax({ url: "/posts/update_counter", // Route to the controller#action type: "POST"

在我的rails应用程序中,我想每10秒用ajax调用一个控制器动作。这很好用。但是,每次执行代码时,页面都会跳回顶部,这是我不希望看到的

这是我目前的代码:

$(document).ready(function(){

  setInterval(function(){
    $.ajax({
            url: "/posts/update_counter", // Route to the controller#action
            type: "POST",
    });
    return false;
  }, 10 * 1000);

});
通过搜索类似的问题,找到了使用
return false
和/或
preventDefault()
的解决方案,但是我无法使它在
setIntervall()中工作

我的帖子\u controller.rb

#Everytime this action is called, some columns should be updated (namely: lowviews, highviews, lowlikes, highlikes)

def update_counter
  @fposts = Fpost.all

  @fposts.each do |fp|
    fp.update_attributes(lowviews: fp.lowviews + rand(5..10), 
                         highviews: fp.highviews + rand(5..10),
                         lowlikes: fp.lowlikes + rand(1..5),
                         highlikes: fp.highlikes + rand(1..5))
  end

  respond_to do |format|
    format.html { redirect_to posts_path }
    format.js 
  end
end
post 'posts/update_counter' => 'posts#update_counter'
在我的路线中。rb

#Everytime this action is called, some columns should be updated (namely: lowviews, highviews, lowlikes, highlikes)

def update_counter
  @fposts = Fpost.all

  @fposts.each do |fp|
    fp.update_attributes(lowviews: fp.lowviews + rand(5..10), 
                         highviews: fp.highviews + rand(5..10),
                         lowlikes: fp.lowlikes + rand(1..5),
                         highlikes: fp.highlikes + rand(1..5))
  end

  respond_to do |format|
    format.html { redirect_to posts_path }
    format.js 
  end
end
post 'posts/update_counter' => 'posts#update_counter'

问题中的代码不会导致跳转到页面顶部。你不小心漏掉了那部分。证据:我已经用我的相关ruby文件更新了我的问题:)。ruby文件完全不相关。控制器响应ajax请求返回什么并不重要。除非你的JavaScript代码中有什么东西在处理导致滚动的响应(你引用的JavaScript代码中没有),否则它返回的任何东西都不会导致滚动。我不知道RoR,但看起来你的响应中有一些JS
format.js
猜猜问题出在哪里?@T.J.Crowder:Mhh这真的很奇怪,因为这是我到目前为止写的唯一的js代码。问题是:Ajax在我的rails控制器中调用controller#action,这个controller#action更新数据库,在不重新加载页面的情况下,我的html视图也会每10秒更新一次。虽然我没有在ajax调用的
success:
部分中指定任何内容。