Thursday, 28 February 2013

Javascript : How many times was the function called

I recently ran into a problem where my Javascript was looping, and constantly calling a function.  As the browser was locking up, I found it very hard find out the cause of the issue.  To help resolve this issue I  wrote the following.

The comments explain how to use it. In short it records every entrance into a function and counts the instances and pops up an "alert" when a proc is called 50 times (the number is adjstable).


            //
            // You should  place the following lines at the top of your page
            //
            // var functionCalls = new Array();             // used to record all the calls
            // var consecutiveCallCount = 0;                // used to check for consecutive calls    
            // var lastFunctionCalled = '';                 // used to check for consecutive calls    
            // var consecutiveCallCountAlertThreshold = 50; // an alert is generated if a method is called this many times consecutively
            //
            // You should also put an empty DIV as show below, somewhere on the page. The 
            // contents of the div will be updated after every function call, and will 
            // highlight the through put of every function.  If you have a serious loop
            // this will not be updated until the consecutive threshold is reached.
            //
            // 
// // Place a call to this function at the top of every function that you want to track // e.g... // function someFoo () { // storeFunctionCalls(); // ... // // This function will count every call to the function and display it to screen. function storeFunctionCalls() { // get the name of the function that called this one var temp = arguments.callee.caller.toString(); var fName = temp.substring(temp.indexOf("function") + 8, temp.indexOf("(")) || "anoynmous"; fName = fName.replace(/^\s+|\s+$/g, ''); // loop through the existing method calls and update the count var indexOfExistingRow = -1; for (var i = 0; i < functionCalls.length; i++) { if (functionCalls[i].Name == fName) { functionCalls[i].Count++; indexOfExistingRow = i; break; } } // the method that called this has not done so before so add it into the collection if (indexOfExistingRow == -1) { functionCalls.push({ "Name": fName, "Count":1 }); } // update the onscreen display of method calls var s = ''; for (var i = 0; i < functionCalls.length; i++) { s += functionCalls[i].Name + ': ' + functionCalls[i].Count + ''; } $('#debugOutput').html(s); // checks for the consecutive calls and alert if we've passed the threshold. if (fName == lastFunctionCalled) { consecutiveCallCount++; if (consecutiveCallCount == consecutiveCallCountAlertThreshold) { alert(consecutiveCallCount + ' consecutive method calls to the same function :' + fName); } } else { lastFunctionCalled = fName; consecutiveCallCount = 0; } }

Monday, 8 August 2011

IE9 Javascript change

I ran into a problem where by a piece of positioning Javascript that worked in all browser including IE 8 did not work in IE9.  "style.top" did not work under IE9 !

The original code was this...

ele.style.top = (a - b);

I had to change it to...

ele.style.tp = (a - b).toString() + 'px';

Wednesday, 18 May 2011

Avoid Collation Issues

I had yet another query where the difference in the collation between my development database and tempdb that caused a TSQL error to be thrown.

The answer was to "collation proof" the query. It was really simple, you add the collation to be used into the query itself! below is the example.

WHERE #RawData.FullName = DiscrepancyReport.FullName COLLATE SQL_Latin1_General_CP1_CI_AS

Monday, 18 April 2011

Encoding images into HTML using C#

Here's a sample that converts a jpeg into an imline image tag...


using System;
using System.IO;
using System.Drawing;
namespace ImageToBase64
{
class Program
{
static void Main(string[] args)
{
using(System.Drawing.Image i = (Image)Bitmap.FromFile(@"c:\test\TMC_logo_2.jpg"))
{
using(MemoryStream ms = new MemoryStream())
{
i.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bytes = ms.ToArray();
string s = Convert.ToBase64String(bytes);
using(TextWriter tw = new StreamWriter(@"c:\test\string.txt"))
{
tw.WriteLine(string.Format(@"", s));
}
}
}
}
}
}

Friday, 11 February 2011

WatiN Samples

Accessing an HTML Table, which gives access to rows and columns

WatiN.Core.Table tbl = this.IE.Table(Find.ById("ctl00_bodyContentPlaceHolder_GridView"));

Typing into text field

this.IE.TextField(Find.ByName("ctl00$bodyContentPlaceHolder$Name")).TypeText(CompanyName);

Thursday, 1 July 2010

Anonymous Types in a RowDataBound event

Using Linq to SQL to populate a grid

protected void ConsignmentGridView_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
var o = e.Row.DataItem;
Type t = o.GetType();
PropertyInfo pi = t.GetProperty("ConsignmentID");
long ConsignmentID = (long)pi.GetValue(o, null);

Thursday, 19 February 2009

Chart Control Problem

I've been banging my head againsta problem with the new Microsoft Charting control for an hour or so.

It turns out that the problem is simply that it can not be invisible!

I originally had the control in a Multiview and the View it was on was not the first view shown, you had to click a button or two and then the chart should appear by setting its view to be the active one. No luck, when it came time to display the view the "Error executing child request for ChartImg.axd" error came up.

So thinking it might be linked to the Multiview, I simply turned the chart invisible. Same problem.

But if I leave the chart on the screen at all time and simply use a style sheet to hide and show it, it works fine. So it seems that the problem is just that if the chart is first invisible ( server-side ) it will crash when you make it visible.

A bit crap really, but never mind, it's done now, phew!