setTimeout函数总是给我带来麻烦.现在我有一个递归的函数(通过setTimeout调用自身)并更改元素高度.
该函数发送两个参数:要更改的元素和元素的最大高度.该功能的目的是展开元素,或以恒定的速度"向下滑动".我知道我可以用jQuery解决这个问题,但我正在尝试自己的功能.
function slide_down(element, max_height)
{
if(element.clientHeight < max_height)
{
var factor = 10;
var new_height = (element.clientHeight + factor >= max_height) ? max_height : (element.clientHeight + factor);
element.style.height = new_height + 'px';
var func_call = 'slide_down(' + element + ', ' + max_height + ');';
window.setTimeout(func_call, 10);
}
}
Run Code Online (Sandbox Code Playgroud)
我在最初调用函数时测试了参数.max_height设置为20,因为它是所需的高度元素(我从.scrollHeight获得此值).
当函数完成后,我想让它自己调用,直到max_height是元素的高度.我用这个setTimeout调用来做到这一点:
var func_call = 'slide_down(' + element + ', ' + max_height + ');';
window.setTimeout(func_call, 10);
Run Code Online (Sandbox Code Playgroud)
它不起作用.这确实有效:
var func_call = 'alert(1);';
window.setTimeout(func_call, 10);
Run Code Online (Sandbox Code Playgroud)
我也尝试将函数调用直接放入setTimeout语句,仍然无法正常工作.
请注意,元素的高度DOES会更改第一次迭代,该函数永远不会调用自身.所以元素变量设置正确,我使用alert()来显示max_height也是正确的.
alert(func_call);
Run Code Online (Sandbox Code Playgroud)
提醒:
slide_down([object HTMLParagraphElement], …Run Code Online (Sandbox Code Playgroud) 我正在努力学习餐饮哲学家问题中信号量的基本要点.现在,我有一个类Chopstick,每个Chopstick都有一个信号量,有1个可用的许可证:
public class Chopstick
{
Thread holder = null;
private Semaphore lock = new Semaphore(1);
public synchronized void take() throws InterruptedException
{
this.lock.acquire();
holder = Thread.currentThread();
}
public synchronized void release()
{
this.lock.release();
holder = null;
}
}
Run Code Online (Sandbox Code Playgroud)
holder变量用于我不确定需要的函数:
public synchronized void conditionalRelease()
{
if (holder == Thread.currentThread())
{
holder = null;
this.lock.release();
}
}
Run Code Online (Sandbox Code Playgroud)
该程序编译并运行,但似乎在释放筷子时遇到一些麻烦.有时,筷子会被释放,有时则不会.当他们不释放时,当所有的筷子被拿走并且一个哲学家饿了时,程序最终会挂断.
这是Philosopher类中的代码,用于在随机的时间之后释放筷子:
System.out.println(this.name + " is eating");
Thread.sleep(this.getRandTime());
System.out.println(this.name + " has finished eating");
rightChopstick.release();
System.out.println(this.name + " has released the right chopstick");
leftChopstick.release();
System.out.println(this.name …Run Code Online (Sandbox Code Playgroud) 我的GUI锁定,因为我需要通过EDT更新它,但是,我还需要通过GUI传递一个正在更新的变量:
while ((message = this.in.readLine()).startsWith("NUMPLAYERS"))
{
numOfPlayers = Integer.parseInt(message.split(":")[1]);
numPlayers.setText("There are currently " + numOfPlayers + " players in this game");
}
Run Code Online (Sandbox Code Playgroud)
这不起作用.我需要在EDT中设置文本,但我不能将numOfPlayers传递给它而不将其声明为final(我不想这样做,因为它随着新玩家加入服务器而改变)
我是python的新手.我正在尝试编写一个程序,在(x,y)处单击鼠标,将其移动到(a,b),然后等到鼠标下的颜色为某种颜色,让我们说#fff.当它是那种颜色时,它再次点击然后重复.
我找不到适合python的鼠标相关内容的API.
我正在编写单元测试来查找某个笔尖上是否存在元素.例如,我想循环遍历一个nibs视图,并检查是否存在引用插件'commentTextView',以检查该文本视图是否存在.
现在,我只看到检查目标是否存在的方法(比如检查按钮在点击时是否会调用某个选择器),而不是检查我需要什么.
我觉得很多文档都已经过时了,但这是我到目前为止所尝试的内容:
我正在使用ActiveMerchant::Billing::PaypalExpressGateway网关.
首先,我设置购买并将用户重定向到Paypal:
response = gateway.setup_purchase price,
return_url: <confirm url>,
cancel_return_url: <cancel url>,
items: [
{
name: 'My Item',
quantity: 1,
description: "My Item Description",
amount: price
}
]
redirect_to gateway.redirect_url_for(response.token)
Run Code Online (Sandbox Code Playgroud)
这是有效的,我可以作为沙盒买家登录并确认付款,这让我<confirm url>从上面回来.在确认中,我做:
response = gateway.recurring price, nil,
token: params[:token],
period: 'Year',
frequency: 1,
start_date: Time.now,
description: 'My Item Subscription'
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我从response变量中的Paypal收到无效的令牌错误.令牌似乎没问题,当我被带回确认网址时,它会出现在网址中.我然后直接(params[:token])将其发送回Paypal.
我做错了什么吗?就像我说的,似乎很多这类过程的文档已经过时(或者我正在尝试的是过时的东西...)
对于编程课程,我正在为第一个家庭作业创建一个二十一点程序.教授给了我们一个示例卡类,其中包括将它们添加到卡组中的方法.对于她的套牌,她使用了一个ArrayList,您可以使用Collections.shuffle()方法轻松地使用Knuth Shuffle.
虽然(显然)这种方法对Stacks不起作用,但我认为Stack结构最适合这个程序,因为你可以弹出并推出卡片进出套牌.
我想用GUI Builder创建一个JButtons数组(不是实际编写代码,而是绘制代码)。我只能弄清楚如何更改元素的名称,而不能将其添加到数组中。
谢谢。
我想知道它是否更快在尝试时,例如,检查论坛上特定帖子中有多少帖子.我是不是该...
(a)使用特定的线程ID浏览数据库中的每个帖子,并计算行数
要么
(b)每次创建一个线程时,在线程数据库中向一个单独的列添加一个,然后查询该单个行
谢谢.
我有一个Database.php类,它是一个抽象的Singleton类:
<?php
abstract class Database
{
protected static $_instance;
...
public static function instance()
{
$class = get_called_class();
if (!self::$_instance) {
self::$_instance = new $class();
}
return self::$_instance;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
其他数据库扩展了这个类并实现了抽象函数,这样我就可以更改我使用的数据库,同时确保我的应用程序仍然有效,只要它使用这个抽象层中的函数(我已经省略了上面的函数定义)码).
我在PDO.php中有一个类PDO.它扩展了数据库,并且还包含instance()与上面相同的功能,但它没有自己的$_instance变量.
class PDO extends Database
{
//...
public static function instance()
{
$class = get_called_class();
if (!self::$_instance) {
self::$_instance = new $class();
}
return self::$_instance;
}
}
Run Code Online (Sandbox Code Playgroud)
最初我以为我不必instance()在PDO类中包含该函数,因为它继承自Database.但是我把它包括在内是因为我收到了这个错误:
Fatal error: Call to undefined method PDO::instance()
问题是,即使使用上面的代码,我仍然会收到此错误.我不知道这是否相关,但我还有另一个奇怪的错误.我想要使用的数据库类型(在本例中为PDO)存储在可访问的变量中App::setting('DB').我的MVC框架中的Model基类加载适当的数据库类并将其存储在其中$this->_db.以下是模型的代码:
<?php
require_once …Run Code Online (Sandbox Code Playgroud) java ×4
php ×2
cocoa ×1
colors ×1
iboutlet ×1
inheritance ×1
javascript ×1
jbutton ×1
locking ×1
mouse ×1
mysql ×1
netbeans ×1
objective-c ×1
optimization ×1
paypal ×1
python ×1
semaphore ×1
settimeout ×1
shuffle ×1
stack ×1
static ×1
xcode ×1