首页 > 解决方案 > Browser refreshes html file twice when using Visual Studio Code with Live Server or Browser-Sync

问题描述

For testing purposes, I have a simple html file that contains two lines of Javascript. I'm using Visual Studio Code (version 1.29.0) with the Live Server extension (version 5.1.1). With the html file loaded in the browser via Live Server, whenever the html file is saved, the browser refreshes (producing a random number in the console). After roughly 1 second, the browser will refresh again (producing a different random number in the console). It does this every time I save the file. The same issue occurs when I try it using Browser-Sync.

I've tried this with Chrome, Firefox, Opera and Edge. They all exhibit the same behavior.

I've been using Visual Studio Code for several months and this wasn't happening until recently that I can remember. I've toggled all of the settings in VSCode that I think might be relevant to the problem, but can't seem to find the issue.

Any ideas as to what is making the browsers refresh twice?

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Javascript Sandbox</title>
</head>
<script>
  var a = Math.floor(Math.random() * 10);
  console.log(a);
</script>
<body>
  <h1>Javascript Sandbox</h1>
</body>
</html>

标签: visual-studio-code

解决方案


我想到了。我使用的是 Windows 10。问题与 OneDrive 有关。我的 html 文件存储在我的 OneDrive 文件夹中的一个文件夹中。我将该文件夹设置为“始终保留在此设备上”,这意味着它存储为本地副本。但是,当我将文件复制到桌面并从那里运行时,浏览器只会刷新一次。


推荐阅读