- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 JavaScript 开发一个简化的机场模拟器,使用非常简单的 CSS 或 JQuery 动画来演示起飞或着陆(例如图像在起飞或着陆时消失)。起飞队列中已经有预定数量的 5 架飞机(呼号代表飞机),着陆队列中已有 5 架飞机。这是我开始的方式
function airport()
{
this.takeoff_queue = ["KQA","ERJ","TOM","DHL","ETH"];
this.landing_queue = ["RWA","KLM","PAN","FLY540","JAMBO"];
this.arrival = function arrival()
this.departure = function departure()
因为只有一条跑道。当到达和起飞时间相同时,着陆飞机应优先。我假设每 1 分钟起飞一次,每 2 分钟降落一次。我计划使用到达和离开函数使用数组 shift()
将每个队列出队。方法。
对于每次出队,我还应该通过制作代表排队飞机的 html div 中的几个图像之一来非常简单地制作动画。最大的问题是我应该如何实现计时功能来运行模拟并在需要时优先考虑到达?另外,我如何合并图像动画?
最佳答案
我喜欢这个用例,所以我做了一个模拟(只是为了好玩)。我希望你不是学生,我现在正在浪费你的作业。
http://jsfiddle.net/martijn/398oezj0/13/
function AirPlane() {
this.state = ko.observable();
this.name = ko.observable();
this.destination = ko.observable();
this.location = ko.observable();
this.arrivesOn = ko.observable();
this.takeOfftime = ko.observable();
this.startTime = null;
// set take off state
this.takeOff = function (runWay) {
this.runWay = runWay;
this.state("Taking off");
this.takeOfftime(60);
}
// set the destination
this.setDestination = function (name, time) {
this.startTime = time;
this.arrivesOn(time);
this.state("Flying");
this.destination(name);
}
// calculate an airplane number
var n = Math.floor(1 + Math.random() * 1000).toString();
while (n.length < 4) {
n = "0" + n;
}
// computed for the progress bar
this.progress = ko.computed(function () {
if (this.state() == "Flying" || this.state() == "Landing") {
if (this.arrivesOn()) {
return Math.floor(100 - ((this.startTime - this.arrivesOn()) / this.startTime) * 100).toString() + "%"
}
} else if (this.state() == "Taking off" && this.takeOfftime()) {
return Math.floor(100 - ((60 - this.takeOfftime()) / 60) * 100).toString() + "%"
}
return null;
}, this);
this.name("PH" + n);
// airplane clock, manages arriveal, landing and takingOff
this.tick = function () {
if (this.arrivesOn()) {
this.arrivesOn(this.arrivesOn() - 1);
if (this.state() == "Flying") {
if (this.arrivesOn() < 60) {
this.state("Landing");
}
} else if (this.state() == "Landing") {
if (this.arrivesOn() <= 0) {
this.state("Landed")
this.location("schiphol");
this.destination("");
this.arrivesOn(null)
}
}
}
if (this.takeOfftime()) {
this.takeOfftime(this.takeOfftime() - 1);
if (this.takeOfftime() <= 0) {
this.state("Flying");
this.destination("Far Far away");
this.runWay.state("Free");
this.location("");
this.arrivesOn(null);
this.runWay = null;
this.takeOfftime(null);
}
}
}
}
// for the future, when we have multiple runways :-)
function RunWay() {
this.state = ko.observable("Free");
}
// Airfield object, future use, if we want to simulate multiple airfields
function AirField(world) {
this.world = world;
this.name = "schiphol";
this.runWay = new RunWay();
// finds the first free runway, we have one, so easy
this.getFreeRunway = function () {
if (this.runWay.state() == "Free") {
return this.runWay;
} else {
return null;
}
}
// finds the first plane that is due to arrive, this includes planes in the landing state
this.getNextArrival = function () {
var arrivalOn;
var arrivalPlane;
var _this = this;
this.world.airPlanes().forEach(function (ap) {
if (
(ap.state() == "Flying" || ap.state() == "Landing") && ap.destination() == _this.name) {
if (!arrivalPlane) {
arrivalPlane = ap;
arrivalOn = ap.arrivesOn();
} else if (ap.arrivesOn() < arrivalOn) {
arrivalPlane = ap;
arrivalOn = ap.arrivesOn();
}
}
});
return arrivalPlane;
}
// Finds a waiting airplane that is ready to depart, it must be on this airfield (duh)
this.getWaitingAirPlane = function () {
var _this = this;
var returnValue;
this.world.airPlanes().forEach(function (ap) {
if (!returnValue && ap.state() == "Waiting" && ap.location() == _this.name) {
returnValue = ap;
}
});
return returnValue;
}
// try to schedule a tack off
// Check if we have 60s free before a plan STARTS landing == arrivesOn -60
// if we have that slot, the plane can takeof
this.scheduleTakeOff = function () {
var nextArrival = this.getNextArrival();
var freeRunway = this.getFreeRunway();
var waitingPlane = this.getWaitingAirPlane();
var landingOn = nextArrival.arrivesOn() - 60;
if (freeRunway && landingOn > 60) {
debugger;
if (waitingPlane) {
freeRunway.state("Occupied");
waitingPlane.takeOff(freeRunway);
}
}
}
}
// World simulation
function World() {
this.airPlanes = ko.observableArray();
this.airField = new AirField(this);
// the ticking clock
this.time = ko.observable(0);
var airPlane;
// load 10 waiting planes
for (var i = 0; i < 10; i++) {
airPlane = new AirPlane();
airPlane.airField = this.airField;
airPlane.state("Waiting");
airPlane.location("schiphol");
this.airPlanes.push(airPlane);
}
// load 10 flying planes, they arrive 120s after each other with 60s random to make things more interesting
var arrivalTime = 0;
for (var i = 0; i < 10; i++) {
airPlane = new AirPlane();
var arrivalTime = arrivalTime + 120 + Math.floor(Math.random() * 60);
airPlane.setDestination("schiphol", arrivalTime);
this.airPlanes.push(airPlane);
}
// run the simulation
this.tick = function () {
this.time(this.time() + 1);
var _this = this;
// update plane progress
this.airPlanes().forEach(function (ap) {
ap.tick();
});
// try to schedule a tack off
this.airField.scheduleTakeOff();
// play with the 100ms here to speedup or slow down
window.setTimeout(function () {
_this.tick();
}, 100);
}
}
// bind the world
var world = new World();
// start the clock
world.tick();
ko.applyBindings(world);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<strong>Time:<span data-bind="text:time"></span></strong>
<br/>
<table class="table table-striped table-condensed">
<thead>
<tr>
<th>#</th>
<th>state</th>
<th>Location</th>
<th>Destination</th>
<th>Arrives/TakesOff</th>
</tr>
</thead>
<tbody data-bind="foreach:airPlanes">
<tr>
<td data-bind="text:name"></td>
<td data-bind="text:state"></td>
<td data-bind="text:location"></td>
<td data-bind="text:destination"></td>
<td class="progress">
<div class="progress-bar" style="height:100%;" data-bind="style: { 'width': progress }, css:{ 'progress-bar-danger': (state()=='Taking off' || state()=='Landing'), 'progress-bar-success':state()=='Flying' }">
<span data-bind="text:arrivesOn"></span> <span data-bind="text:takeOfftime"></span>
</div>
</td>
</tbody>
</table>
关于javascript - 使用 JavaScript 进行机场模拟,有固定数量的飞机等待起飞和降落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28320776/
我有一个 html 格式的表单: 我需要得到 JavaScript在value input 字段执行,但只能通过表单的 submit .原因是页面是一个模板所以我不控制它(不能有
我管理的论坛是托管软件,因此我无法访问源代码,我只能向页面添加 JavaScript 来实现我需要完成的任务。 我正在尝试用超链接替换所有页面上某些文本关键字的第一个实例。我还根据国家/地区代码对这些
我正在使用 JS 打开新页面并将 HTML 代码写入其中,但是当我尝试使用 document.write() 在新页面中编写 JS 时功能不起作用。显然,一旦看到 ,主 JS 就会关闭。用于即将打开的
提问不是为了解决问题,提问是为了更好地理解系统 专家!我知道每当你将 javascript 代码输入 javascript 引擎时,它会立即由 javascript 引擎执行。由于没有看过Engi
我在一个文件夹中有两个 javascript 文件。我想将一个变量的 javascript 文件传递到另一个。我应该使用什么程序? 最佳答案 window.postMessage用于跨文档消息。使
我有一个练习,我需要输入两个输入并检查它们是否都等于一个。 如果是 console.log 正则 console.log false 我试过这样的事情: function isPositive(fir
我正在做一个Web应用程序,计划允许其他网站(客户端)在其页面上嵌入以下javascript: 我的网络应用程序位于 http://example.org 。 我不能假设客户端网站的页面有 JQue
目前我正在使用三个外部 JS 文件。 我喜欢将所有三个 JS 文件合而为一。 尽一切可能。我创建 aio.js 并在 aio.js 中 src="https://code.jquery.com/
我有例如像这样的数组: var myArray = []; var item1 = { start: '08:00', end: '09:30' } var item2 = {
所以我正在制作一个 Chrome 扩展,它使用我制作的一些 TamperMonkey 脚本。我想要一个“主”javascript 文件,您可以在其中包含并执行其他脚本。我很擅长使用以下行将其他 jav
我有 A、B html 和 A、B javascript 文件。 并且,如何将 A JavaScript 中使用的全局变量直接移动到 B JavaScript 中? 示例 JavaScript) va
我需要将以下整个代码放入名为 activate.js 的 JavaScript 中。你能告诉我怎么做吗? var int = new int({ seconds: 30, mark
我已经为我的 .net Web 应用程序创建了母版页 EXAMPLE1.Master。他们的 I 将值存储在 JavaScript 变量中。我想在另一个 JS 文件中检索该变量。 示例1.大师:-
是否有任何库可以用来转换这样的代码: function () { var a = 1; } 像这样的代码: function () { var a = 1; } 在我的浏览器中。因为我在 Gi
我收到语法缺失 ) 错误 $(document).ready(function changeText() { var p = document.getElementById('bidp
我正在制作进度条。它有一个标签。我想调整某个脚本完成的标签。在找到可能的解决方案的一些答案后,我想出了以下脚本。第一个启动并按预期工作。然而,第二个却没有。它出什么问题了?代码如下: HTML:
这里有一个很简单的问题,我简单的头脑无法回答:为什么我在外部库中加载时,下面的匿名和onload函数没有运行?我错过了一些非常非常基本的东西。 Library.js 只有一行:console.log(
我知道 javascript 是一种客户端语言,但如果实际代码中嵌入的 javascript 代码以某种方式与在控制台上运行的代码不同,我会尝试找到答案。让我用一个例子来解释它: 我想创建一个像 Mi
我如何将这个内联 javascript 更改为 Unobtrusive JavaScript? 谢谢! 感谢您的回答,但它不起作用。我的代码是: PHP js文件 document.getElem
我正在寻找将简单的 JavaScript 对象“转储”到动态生成的 JavaScript 源代码中的最优雅的方法。 目的:假设我们有 node.js 服务器生成 HTML。我们在服务器端有一个对象x。
我是一名优秀的程序员,十分优秀!