我正在尝试使用console.log将一些日志记录放入我程序的javascript端.但是我注意到,除非在IE中打开开发控制台,否则JS在命中console.log时基本停止工作.这是一个痛苦......这意味着每当我想进行生产构建时,我必须删除所有日志记录.
除了显而易见的:
function DoSafeConsoleLog( parameters )
{
if ( !$.browser.msie )
{
console.log( parameters );
}
}
Run Code Online (Sandbox Code Playgroud)
有没有一种很好的方法来记录对所有主流浏览器友好的JavaScript?
编辑:
那么,在查看重复的帖子(oops)以及在这里考虑答案之后,我必须在调用之前检查控制台是否存在.尽管我不愿意获得额外的标记,但我宁愿不要踩到可能想要使用Firebug Lite调试代码的未来程序员.
我刚碰到这个代码,我不明白.是否有理由使用此设计而不是仅使用AutoReset重新运行已用代码?
private readonly Timer Timer = new Timer();
protected override void OnStart(string[] args)
{
Logger.InfoFormat("Starting {0}.", ServiceName);
try
{
// If Enabled is set to true and AutoReset is set to false, the Timer raises the Elapsed event only once, the first time the interval elapses.
Timer.AutoReset = false;
Timer.Elapsed += Timer_Elapsed;
Timer.Interval = Settings.Default.ScriptingStatusLifeTime;
Timer.Start();
}
catch (Exception exception)
{
Logger.ErrorFormat("An error has occurred while starting {0}.", ServiceName);
Logger.Error(exception);
throw;
}
}
/// <summary>
/// Whenever the Schedule Service …Run Code Online (Sandbox Code Playgroud) public ActionResult CustomChart(int reportID)
{
Chart chart = new Chart();
// Save the chart to a MemoryStream
var imgStream = new MemoryStream();
chart.SaveImage(imgStream);
imgStream.Seek(0, SeekOrigin.Begin);
// Return the contents of the Stream to the client
return File(imgStream, "image/png");
}
Run Code Online (Sandbox Code Playgroud)
我习惯于将"using"语句与MemoryStreams结合使用.这是不是必须使用'使用'语句的情况吗?或者在'using'语句中调用return是否有效?
编辑:
出于我的目的,我发现引入'using'语句不起作用(抛出ObjectDisposedException).这是我在客户端做的事情:
$('#ReportTest').bind('load', function () {
$('#LoadingPanel').hide();
$(this).unbind('load');
}).bind('error', function () {
$('#LoadingPanel').hide();
$(this).unbind('error');
}).attr('src', '../../Chart/CustomChart?ReportID=' + settings.id);
Run Code Online (Sandbox Code Playgroud) 我最近为Visual Studio安装了NodeJS Tools,它支持VS中的Node环境.值得注意的是,它能够从IDE设置调试断点.
我不清楚在调试Gulp任务时是否可以设置断点.Task Runner能够检测Gulp任务并将console.log语句输出到窗口,但我还没有找到更好的调试方法.
我在一段时间后发现了这篇文章:如何在使用Visual Studio Task Runner Explorer运行时调试gulpfile.js?但是,这篇文章不涉及NodeJS Tools for VS. 所以,我正在重新提出问题以考虑该插件.
Resharper刚刚提示我这行代码:
private static bool shouldWriteToDatabase = false;
Run Code Online (Sandbox Code Playgroud)
表示我不应该说"= false",因为bools显然在C#中默认为false.我用C#编程超过一年半,从来不知道.我想它只是在裂缝中滑落,但这让我想知道什么是好的做法.
我是否假设所有人都能理解默认值?这将导致更清晰的代码,但如果另一个程序员不知道默认值,则会引起歧义.
我正在尝试正确验证并重置表单.验证工作正常 - 成功监视所需的字段.但是,当我重置表单时 - 我只看到输入的红色背景清晰,而不是验证消息.
根据jQuery验证文档:
将输入字段重置为其原始值(需要表单插件),删除指示无效元素的类并隐藏错误消息.
以下是我认为与该问题相关的所有信息.如果您想看到其他任何内容,请告诉我.
这是我在模型中生成DOM元素的方法.此元素需要验证:
//Model.ascx
<div class="detailsRow required">
<%= Html.LabelFor(model => model.Site) %>
<%= Html.DropDownListFor(model => model.SelectedSiteID, new SelectList(Model.Site, "Key", "Value"))%>
<%= Html.ValidationMessageFor(model => model.SelectedSiteID)%>
</div>
//Model.cs
[DisplayName("Site")]
public List<KeyValuePair<int, string>> Site { get; set; }
[Range(0, int.MaxValue, ErrorMessage = "Site required")]
public int SelectedSiteID { get; set; }
Run Code Online (Sandbox Code Playgroud)
站点是一个以值-1开头的选择列表.任何其他选择都是有效的.因此,我验证了从0到最大的范围.
在JavaScript中,当用户按下表单上的"提交"按钮时,我会针对我的表单运行以下代码:
var form = workflowDialogContent.find('form');
$.validator.unobtrusive.parse(form);
//Maintain a reference to the current formValidator to be able to reset.
var formValidator = …Run Code Online (Sandbox Code Playgroud) 这个问题似乎与这篇文章有关,但我无法从该帖子推断出一个解决方案.
我在我继承的应用程序中注意到这个代码(在日志文件中注意到正在吃异常):
protected void Session_End(object sender, EventArgs e)
{
try
{
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
//if (this.Context.Handler is IRequiresSessionState || this.Context.Handler is IReadOnlySessionState)
//{
// FormsAuthentication.SignOut();
// FormsAuthentication.RedirectToLoginPage();
//}
}
catch (Exception ex)
{
this.GetType().GetLogger().Error(ex);
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道一些事情.首先,SignOut如何抛出空引用异常?这是一个例外情况,还是我在我的计划中做了一些天生错误的事情?接下来,在抛出此异常之前,我应该测试什么来阻止它?
15:51:57,288 [13]错误ASP.global_asax - System.NullReferenceException:未将对象引用设置为对象的实例.在MvcApplication.Session_End的System.Web.Security.FormsAuthentication.SignOut()处
谢谢
我正在学习观察Array对象.我发现以下令人惊讶:
var fooArray = [];
Array.observe(fooArray, function(changes){
console.log('changes:', changes[0].type);
});
fooArray.push({});
Run Code Online (Sandbox Code Playgroud)
导致变化的类型splice与否add
哪种方法会导致类型的更改事件add?在我看来,推动单一价值将是最可能的情况.
刚在我们的应用程序中找到了这段代码片段.我想知道第一行是否是多余的 - 如果它被立即覆盖,是否需要在变量上调用clearTimeout?还是有一些我应该注意的情况?
function Countdown() {
clearTimeout(sessionTimeoutHandle);
sessionTimeoutHandle = setTimeout(function () { countdownHandler() }, MILLISECONDS);
}
Run Code Online (Sandbox Code Playgroud)
我的预感是"是的,你需要调用clearTimeout"因为我无法想象为什么只有将timeout变量设置为null才能存在clearTimeout方法.
我想更好的问题是:
var timeoutHandler = setTimeout(countdownHandler, MILLISECONDS);
timeoutHandler = setTimeout(countdownHandler, MILLISECONDS);
Run Code Online (Sandbox Code Playgroud)
我现在有两个功能等待大约MILLISECONDS,或只有一个吗?
我希望能够显示一些文本,但也可以通过jQuery修改文本.
<%= Html.DisplayFor(model => model.DeviceComponentName)%>
Run Code Online (Sandbox Code Playgroud)
如果我使用EditorFor而不是DisplayFor,我会看到输入控件的ID.不过,我不希望以这种方式编辑值.所以,我把它变成了DisplayFor,但它没有为元素生成ID属性.
我应该将DisplayFor包装在div中并执行以下操作:
<div id="<%= ViewData.TemplateInfo.GetFullHtmlFieldName("DeviceComponentName") %>">
<%= Html.DisplayFor(model => model.DeviceComponentName)%>
</div>
$('#DeviceComponentName').text('newValue');
Run Code Online (Sandbox Code Playgroud)
或者有更清洁的方法来实现这一目标吗?
更新:有没有一种方法不依赖于硬编码字符串?与对象本身有关系的东西,所以如果我的属性名称改变,我会得到一个编译错误?
此外,我使用此代码,但我没有看到ID值出现:
<td class="editableValue">
<%--Label should be editable, so it needs an ID, but only will be edited by jQuery so it can't be an EditorFor--%>
<%= Html.DisplayFor(model => model.DeviceComponentName, new { id = "DeviceComponentName" })%>
<button type="button" id="ComponentTreeButton" class="nestedDialogButton">...</button>
</td>
Run Code Online (Sandbox Code Playgroud) c# ×4
jquery ×4
asp.net-mvc ×3
javascript ×3
arrays ×1
gulp ×1
logging ×1
memorystream ×1
node.js ×1
ntvs ×1
settimeout ×1
timer ×1
using ×1
validation ×1